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:
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.
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.