Hey, I am running into an issue with using SetActive() to deactivate and then reactivate a game object. I am running Unity version 4.0.1f2. I’ve taken a basic cube and added the following script to it:
using UnityEngine;
using System.Collections;
public class VisibilityChangeScript : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
print (this.gameObject.activeSelf);
if (Input.GetKey(KeyCode.Q))
{
this.gameObject.SetActive(false);
}
else if (Input.GetKey(KeyCode.E))
{
this.gameObject.SetActive(true);
}
}
}
When I hit the Q key, the cube becomes inactive and it stops printing the cube’s activity to the console. However, when I hit the E key, the cube never becomes active again.
Am I missing something really simple here? Is it possibly that the object is getting removed completely from the system, so it can’t actually be reactivated?
n/m, I was able to figure this one out pretty quickly. Wasn’t thinking to straight.
For those that run into this issue, this will never work because one the game object becomes inactive, it’s Update() method will not be called. So, the SetActive(true) can never be reached and the object will remain inactive.
To fix this, you need to have a parent game object that contains all the child game objects you want to make inactive and reactive. Then, add the following script to the parent:
using UnityEngine;
using System.Collections;
public class VisibilityChangeScript : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
foreach (Transform child in this.transform)
{
if (Input.GetKey(KeyCode.Q))
{
child.gameObject.SetActive(false);
}
else if (Input.GetKey(KeyCode.E))
{
child.gameObject.SetActive(true);
}
}
}
}
Here are two buttons I have. The “transaction_button” will set the “transaction_canvas” to setactive(false). The “outlook_button” will set the “transaction_canvas” to setactive(true).
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class transaction_button : MonoBehaviour {
void Start()
{
Button btn = this.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Camera.main.backgroundColor = new Color32(64, 161, 81, 255);
//GameObject.Find("Title_Text").GetComponent<Text>().text = "Expense Transaction";
GameObject.Find("Transaction_Canvas").SetActive(false);
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class outlook_button : MonoBehaviour {
GameObject testObject;
void Start()
{
testObject = GameObject.Find("Transaction_Canvas");
Button btn = this.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Camera.main.backgroundColor = new Color32(61, 136, 188, 255);
testObject.SetActive(true);
}
}
you’ll notice that the “transaction_button” doesn’t have the game object variable, so it will only work if the object i’m “finding” is active. This is a bad idea and will give the following error if clicked twice in a row (without clicking the “outlook_button”). NullReferenceException: Object reference not set to an instance of an object
Old discussion. I actually referred to this discussion, while trying to solve issue where I was unable to activate an inactive gameobject from the update function. I think several people have explained why this can be an issue. I am writing, because I finally solved this, using a solution I was unable to find anywhere else. so, just in case any one else encounters similar issue: I believe I was unable to activate the deactivated gameobject because I started the script with the object deactivated (unchecked). I think this meant that the object was not being found in the Start function and it certainly would not activate in the Update function.
I solved this by starting with the gameobject ACTIVE. then I deactivated it near the beginning of the script, after the object had been found (GameObject.Find).
After this, I had no problem reactivating the game object!
The script switches between two player characters (in-game). Both characters have a “Main Camera” and a “Free Look Camera Rig” attached. cam3 & rig2 (see code below) are the two game objects I had to deactivate/reactivate, in order to successfully switch the 2 characters. Here’s the code I used…
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
using UnityStandardAssets.Cameras;
public class CharacerSwitch : MonoBehaviour
{
public GameObject player1;
public GameObject player2;