Enabling game objects with tag

I’m having some trouble enabling game objects which are child objects of ‘Level 2’ with a tag called ‘Level 2’.
This is the code I used:

GameObject[] gameObjectArray = GameObject.FindGameObjectsWithTag ("Level 2");
foreach(GameObject go in gameObjectArray)
    {
        go.SetActive (true);
    }

The code works to disable objects when ‘true’ is changed to ‘false’.

Since it only works for disabling the objects, I’m pretty sure that the FindGameObjectsWithTag() function only finds active GameObjects. Once you set them to false, it can’t find the objects to set them to active.

You could fix this by creating the array as a public variable, and setting the array to all the objects in the Start() function. That way, the array will only be set once, and then it has all the GameObjects stored, even after they’re disabled.

How would I go about doing this?

public GameObject[] gameObjectarray;

void Start()
{
gameobject array = Gameobject.FindGameObjectsWithTag("Level 2");
foreach(Gameobject go in gameobject array)
{
  go.SetActive(false);
  //Now you can enable them whenever you want
}
}

I would get around this by seting all of them active and in the start function find them and disable them. Then you can activate them when ever you want

That’s what it’s currently set to do.

In your start method?

It was something like that. Your one just gives me errors ‘CS1026: )’, ‘CS1002: ;’, and "CS1513: }’ on line 6.

Yes that’s because I wrote Gameobject instead of GameObject and gameobject array instead of gameObjectarray

That’s solved the errors but the gameobjects still don’t activate.

How do you try to activate them?

You’d use the same array to loop through and activate them. If you deactivate them, you can’t use FindGameObjectsWithTag to find them again. You need to use the previous references to them you got before deactivating them.

I’ve figured out what the problem is. My code disables the array right after it’s activated so I need to clear it before that.