FindSceneObjectsOfType ignoring the inactive gameobjects.. why ?

Hi all

I need to get all the gameobjects in the scene even the inactive ones…

Any idea how I can do that ?

At the moment I use the FindSceneObjectsOfType function but it does give me only the active ones.

And it looks like there’s no parameter to customize the function to return everything…

Any idea ? This sounds like something pretty easy / obvious so I hope someone found a solution or there’s a workaround inside unity for this kind of need !

Thanks in advance for any help !

ps: I know this function is slow but this is for a tool so I don’t really care about speed/perf :wink:

Resources.FindObjectsOfTypeAll loads project assets, including prefabs, which can cause problems especially if you want to modify the results. As an alternative to Resources.FindObjectsOfTypeAll, you can now use Unity’s Scene Manager to find objects, like so:

/// Use this method to get all loaded objects of some type, including inactive objects. 
/// This is an alternative to Resources.FindObjectsOfTypeAll (returns project assets, including prefabs), and GameObject.FindObjectsOfTypeAll (deprecated).
public static List<T> FindObjectsOfTypeAll<T>()
{
	List<T> results = new List<T>();

	for(int i = 0; i< SceneManager.sceneCount; i++)
	{
		var s = SceneManager.GetSceneAt(i);
		if (s.isLoaded)
		{
			var allGameObjects = s.GetRootGameObjects();
			for (int j = 0; j < allGameObjects.Length; j++)
			{
				var go = allGameObjects[j];
				results.AddRange(go.GetComponentsInChildren<T>(true));
			}
		}
	}

	return results;
}

This is useful if you want to modify loaded objects without affecting prefabs and other assets.

Note: You can replace SceneManager with EditorSceneManager if needed.

Last tested in Unity 5.6.0.

I know this is a very very very old post but I was looking for a way to do what the OP wanted and found this in the reference docs.

FindObjectsOfTypeAll

Based on the description it should return every object even if its disabled.

HEADS UP guys – use rempelj 's answer above (NOT the best answer)

Use SceneManager and not Resources because there’s currently multiple unaddressed bugs that will PERMANENTLY change your prefabs and scene!

Posting as an answer to get more attention because this is some really serious stuff.

You can’t.

Either you:

  • Keep a reference to all the inactive objects and access them through the reference

  • Or you create a root gameobject and parent all the other gameobjects in the scene to it, then use GetComponentsInChildren(Transform,true) on it and iterate through all the components returned calling .gameObject on each one

Current asnwer is old and outdated…

From the docs: Unity - Scripting API: Object.FindObjectOfType
public static T FindObjectOfType(bool includeInactive);

Use true if you want to search inactive objects aswell:
FindObjectOfType(true);

Not sure when it got added, but i ended up here, and the solution was much easier than all the other answers here.

var results = FindObjectsOfType<Animator>(true);

the bool parameter is to includeInactive gameobjects.

var currentlyenabled = GameObject.FindWithTag (“enabled”);
var currentlydisabled = GameObject.FindWithTag (“disabled”);

In theory this plus FindGameObjectsWithTag equivalent should work. I tested it and it currently kills Unity Editor. However a guy from Unity promised it should be fixed in Unity 3.5.5