- Home /
how do i use gameObject.setactive with a bool?(my codes not working!)
**Basically i have a bool called switch that i'm getting from another script and i want a platform to be set as inactive when the bool is true, and active when the bool is false. i can get it to go inactive but i cant get it back to active. hope you guys can help help me out its doing my head in! i had a similar thing working with layer collisions but i think this is the better way to go :)
here's my code: **
using UnityEngine;
using System.Collections;
public class SwitchBlue : MonoBehaviour {
public GameObject player;
private ActionManager Manager;
// Use this for initialization
void Start () {
Manager = player.GetComponent<ActionManager>();
}
// Update is called once per frame
void Update () {
if (Manager.Switch == false)
{
gameObject.SetActive (true);
}
else if (Manager.Switch == true)
{
gameObject.SetActive(false);
}
}
}
thanks guys!
I've converted my comment to answer. You can select it as an answer if you want. :-)
Answer by HarshadK · Oct 20, 2014 at 06:44 AM
No script on game object will execute after it is inactive.
So this part of your script
if (Manager.Switch == false)
{
gameObject.SetActive (true);
}
will never execute since your game object is inactive.
You need to do it from an another script where you get the reference to your game object and then active or inactive it from there.
Answer by RedDevil · Oct 20, 2014 at 06:45 AM
Once you disable the item the script is on, you cannot enable it again because there is no call to enable it since the item is disabled.You need the script that enables and disables the object on a different object.