How do I set reference active/not active?

I am currently looking to find a particular GameObject and apply it to a variable, then set it active/not active via the variable. There are probably better ways to do this, but here’s the code I’ve got so far

(in the Start method)

currentScene = GameObject.Find(“/TheGenerator/00”);

(in the Update method)

if (var > 10) {

currentScene.SetActive(false);

currentScene = GameObject.Find(“/TheGenerator/10”);

currentScene.SetActive(true);

This doesn’t work because it’s trying to set the variable active, not the GameObject itself.

The code is formatted in a bad way, next time you can copy/paste it using the “code sample” tool when you write your post, so that it can be more readable for everyone.


Going back to your question:

it’s trying to set the variable active, not the GameObject itself

This sentence is not true, this thing cannot exist in any way.
When a variable contains a class or an object, it’s always a reference, so if you’re doing something on that variable, you’re doing it on the referenced object.

First of all, since I can’t see where you define your variable, make sure that you define it to be of type GameObject, otherwise that would be a first problem.

Once you’re sure the variable is defined correctly, what’s wrong with your code can be understood by reading the documentation page of GameObject.Find.

In particular I’m referring to this line from the documentation:

This function only returns active GameObjects. If no GameObject with name can be found, null is returned.

This means that when you do this

currentScene.SetActive(false);
currentScene = GameObject.Find("/TheGenerator/10");
currentScene.SetActive(true);

On the second line you’re searching for (I assume) an inactive GameObject, which results in getting a null value, so you don’t get the reference to your “/TheGenerator/10” object.
The third line won’t work because you don’t have reference to any gameobject inside currentScene at that point.


Long story short: you can’t search for an inactive GameObject with Find (or any other method under GameObject class). You have to find a workaround for it to be possible, which means you have to find a way to keep reference of all the GameObjects you’re interested in being activated in run-time.

There are many ways to accomplish this, how to do it is up to you, based on what’s more efficient/convenient to do.

  1. One way would be, if you already have the GameObjects in your scene, to setup your script to have a reference to those GameObject from the beginning, by instantiating a variable for each GameObject and assigning it from the editor, with the help of the SerializeField annotation.
  2. Another way would be, if you’re spawning those GameObjects in run-time through a script, to keep, for instance, an array on GameObjects where you put all the references to those GameObjects.

A third way you could achieve this, maybe simpler if this is your case. It seems like you have a TheGenerator object with many childs, as far as I’ve understood (I’m assuming this since youre searching for “/TheGenerator/xx”).
You could do something like this:

`GameObject theGenerator = GameObject.Find("TheGenerator");`

  `GameObject currentScene = theGenerator.transform.getChild(index).gameObject;`

This way you get the child at position index under the TheGenerator GameObject. Rememer that when you get the child you’re getting the Transform, not the GameObject, that’s why I added “.gameObject” at he end of the second line, and you can set active it eventually.

This way you’re also avoiding using GameObject.Find, which is an expensive method to call in run-time, particularly if you’re doing multiple times in Update like I guess you’re trying to do.