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:

7 Answers

7

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.

It works well! thanks for share

@rempelij I am getting the following err when I implemented this: ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.

This solution is great! But it can't find the objects in "DontDestroyOnLoad" scene. Still can't find the perfect solution :(

Oneliner: public static List<T> FindObjectsOfTypeAll<T>() { List<T> results = new List<T>(); SceneManager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren<T>())); return results; }

It ONLY gets root gameObjects

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.

It really includes all objects in memory including much of internal stuff not present in original scene. So if you can differentiate objects you need from others it will do the job.

It's worth noticing that you can tell if an object was instantiated after the scene started by checking its GetInstanceID() which will be positive for things created dynamically and negative otherwise. Other than that as @ksa says there are a lot of things returned including editor objects, preview cameras etc.

Link is broken, heres an updated link: http://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html

Current asnwer is old and outdated.. From the docs: https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html public static T FindObjectOfType(bool includeInactive); Use true if you want to search inactive objects aswell: FindObjectOfType(true);

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

thanks.. the first solution wouldn't work in my case but I guess I can do what you suggest with the root gameobject... but still I'd to know (from unity point of view) what the interest of having this function ignoring the inactive gameobjects.. would that be possible to add in 3.5 that is coming pretty soon, a bool parameter to this function so we can specify if we want all the gameobjects or not (or another function.. whatever..) it doesn't seem to be a big change.. any idea how to request a new feature ? are the awesome unity coders coming on unity answers sometimes ?

to be a bit more precised I'm working on a tool so I don't really want to force all my future users to create an empty root gameobject otherwise the tool won't work.. it would be a bit ugly..

Make a wish: http://feedback.unity3d.com/forums/15792-unity The reason it ignores inactive gameobjects? Because, by design, inactive gameobjects don't exist for all intends and purposes, other then to be activated by something that holds their reference. It'd be stupid if they weren't ignored by these kinds of functions, because then you'd have to check for .active and the function would be significantly slower. I probably shouldn't but.. "the first solution wouldn't work in my case".. why? That probably means you're approaching this wrong.

hum.. ok basically I'm working on some sort of visual editor and I need to access all the gameobjects of the current scene (active or not) so I can edit them directly, group them etc.. and also activate / inactivate them.. the only way I've found is via the FindSceneObjects.. function is there another way to get a reference on gameobjects ?

I have the same problem. Are there any updates on this? The problem was mentioned earlier. I want to have editor script that will do something with objects on the scene (both enabled and disabled). How to access the list of all game objects?

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