How to enable/disable scripts in 1 gameObject? - c#

I have gameObject MainCamera and there is script LookAtCamera as my main view option and I have script MouseLook that I wanna make secondary with right-click.
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
LookAtCamera.enabled = false;
MouseLook.enabled = true;
}
}
}
How to declare script as a public component of MainCamera? I just have one of them enabled and switch between with mouse right click.

You've got most of it done. So, declare the scripts as a public variable like below, and then assign them on the inspector:
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
public LookAtCamera lookAtScript;
public MouseLook mouseLookScript;
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
lookAtScript.enabled = false;
mouseLookScript.enabled = true;
}
}
}

Thx :) I didn't know how to declare script. I also added else to go back to default camera setup.
using UnityEngine;
using System.Collections;
public class CameraManager : MonoBehaviour {
public LookAtCamera lookAtScript;
public MouseLook mouseLookScript;
void Update () {
if (Input.GetKey (KeyCode.Mouse1)) {
lookAtScript.enabled = false;
mouseLookScript.enabled = true;
} else {
mouseLookScript.enabled = false;
lookAtScript.enabled = true;
}
}
}

Related

Unity script work on Editor but not working in Android

Hello I'm game developer one day when I want to test my game on android I join to my game but when I press on buttons public void function doesn't work I fixed it by add to scripts what using buttons onClick.AddListener() but one problem IAP don't work with onClick.AddListener() you can help me fix problem with public void function what works on Editor but don't work in Android when I press on buttons. Thanks.
Script with AddListener [This script where I add onClick.AddListener() to fix this problem but it's be uncomfortable thats why I want to fix this]:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameButtonManager : MonoBehaviour
{
public GameObject DonationShop;
public GameObject ShopBtn;
public GameObject QuitShopBtn;
public GameObject QuitBtn;
public bool CanPress = true;
void Update()
{
ShopBtn.GetComponent<Button>().onClick.AddListener(EnableShop);
QuitShopBtn.GetComponent<Button>().onClick.AddListener(DisableShop);
QuitBtn.GetComponent<Button>().onClick.AddListener(BackToMenu);
}
public void EnableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(true);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
public void BackToMenu()
{
SceneManager.LoadScene(1);
}
public IEnumerator CanPressEnable()
{
yield return new WaitForSeconds(5.5f);
CanPress = true;
}
public void DisableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(false);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
}

Executing a Function Between Script Files

Tell me please. I have 2 scripts that hang on different Unity objects. The first script is for clicking a button. The second one is for executing the function.
How can I execute the AddItem function if the button is pressed?
Script 1 (button click):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
Script 2 (Adding Items):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class InventoryManager : MonoBehaviour
{
public void AddItem(ItemScriptableObject _item, int _amount)
{
foreach(InventorySlot slot in slots)
{
if (slot.item == _item)
{
slot.amount += _amount;
return;
}
}
foreach(InventorySlot slot in slots)
{
//print(_item+" "+_amount);
if (slot.isEmpty == true)
{
slot.item = _item;
slot.amount = _amount;
slot.isEmpty = false;
slot.SetIcon(_item.icon);
return;
}
}
}
If you dont have a variable on the second script, of the first script, you should do that by using:
GameObject go;
go.GetComponent<InventoryManager>();
try changing the first script to something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class UseButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
public bool isClick = false;
void Update()
{
if (isClick)
{
Debug.Log("do something")
}
}
public void OnPointerDown(PointerEventData ped)
{
isClick = true;
}
public void OnPointerUp(PointerEventData ped)
{
isClick = false;
}
}
This way you can modify the variable: is Click easily.

OnCollisionEnter() not working when two GameObjects collide

This code here doesn't work, I don't get any errors, but the scripts doesn't print "End" in the console when my player hits something, here's the code:
using UnityEngine;
public class GameManager : MonoBehaviour
{
public void End(){
Debug.Log("End");
}
}
and on another script:
using UnityEngine;
public class PlayerCollision3 : MonoBehaviour
{
public Move movement;
void OnCollisionEnter (Collision collisionInfo) {
if (collisionInfo.collider.tag == "obsik"){
movement.enabled = false;
FindObjectOfType<GameManager>().End();
}
}
}

Trying to add select button on my controller to open pause menu

So i tryied to add the select button on controller to my script so i can access it with a controller but I don't under stand how because I'm new to coding and if you can explain that would be great.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PauseMenu : MonoBehaviour{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
private bool pauseEnabled;
void Update() {
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}else
{
Pause();
}
}
}
void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
}
First you have to know which button ID's of gamepad/ joystick. I prefer using this package for getting the button ID's of any new gamepad/ joystick. And then by getting the ID's you have to modify the Input Manager if required and call the function, like for example
if(Input.GetKey(KeyCode.Joystick1Button10)){
// do something
}

I'm working on a project but when i try to play it i have the error CS0031 Constant value true cannot be converted to a float

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour {
public AudioSource theMusic;
public bool startPlaying;
public BeatScroller theBS;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (!startPlaying)
{
if (Input.anyKeyDown)
{
startPlaying = true;
theBS.hasStarted = true; <----------CS0031 error
theMusic.Play();
}
}
}
}
THE ERROR IS AT THE LINE WHERE IT SAYS theBS.hasStarted = true;
theBs.hasStarted is a float and you are trying to assign it a boolean.

Categories