GameObject.SetAcive(true); not functioning

I have the following function which disables everything in the scene except GameObjects with the tags MainCamera, Player, and Don’tDestroy. I have a public variable isVoid which stores whether the scene is “in void” (almost everything disabled.) I can get GameObject.SetActive(isVoid); to function when isVoid = true, but when it is false I cannot get everything to re-appear. Here is my function:

    public void disableWorld () {
        GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>();
        isVoid = !isVoid;
        Debug.Log (isVoid);
        if (isVoid) {
            cameraComponent.backgroundColor = backgroundColor;
        } else {
            cameraComponent.backgroundColor = Color.black;
        }
        foreach (GameObject item in allObjects) {
            if (item.tag != "MainCamera" && item.tag != "Player" && item.tag != "Don'tDestroy") {
                item.SetActive (isVoid);
                Debug.Log (item.tag + " " + isVoid + " " + item.activeSelf);
            } else {
                if (item.tag == "MainCamera") {
                    item.GetComponent<mapgenerator> ().enabled = isVoid;
                }
            }
        }
    }

FindObjectsOfType only returns active objects by design.

I’d suggest establishing references to everything you want to enable/disable and iterating over the list rather than using FindObjectsOfType. That way your disabled objects will still be in the list.

1 Like

What do you mean “by design?” Thanks! Sorry if this is a dumb question. :sweat_smile:

Nevermind, I see what you are saying. How do I retrieve all objects in scene, even inactive ones? Thanks! Sorry about the misunderstanding.

Read the description part

1 Like

Thank you!! I used Resources.FindObjectsOfTypeAll and that worked perfectly. Brilliant.

However, got some funny stuff when changing the status of that. Probably because it affects all assets in project, made Unity crash three times. I’m going to go through all children of an overarching parent class instead.

You can write the function yourself. Your scene has a GetRootGameObjects function, that gives you all of the root objects. Then you go through all of their children recursively.

A very simple version would be:

public List<GameObject> GetAllGameObjects() {
    List<GameObject> allGameObjects = new List<GameObject>();
    var currentScene = SceneManager.GetActiveScene();
    foreach(var obj in currentScene.GetRootGameObjects()) {
        AddObjectAndChildrenTo(allGameObjects, obj);
    }
    return allGameObjects;
}

private void AddObjectAndChildrenTo(List<GameObject> objects, GameObject obj) {
    objects.Add(obj);
    for(int i = 0; i < obj.transform.childCount; i++) {
        AddObjectAndChildrenTo(objects, obj.transform.GetChild(i).gameObject);
    }
}

Note that this is really slow and you shouldn’t do it often. If you can, collect the objects once, filter out objects you won’t care about, and iterate the same list again.

1 Like

Thank you! This was the easiest method for me. :smile: