- Home /
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 ;)
Answer by alexfeature · May 06, 2012 at 08:40 AM
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.
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);
Answer by rempelj · Nov 16, 2016 at 07:26 AM
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.
@rempelij I am getting the following err when I implemented this:
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from $$anonymous$$onoBehaviour or Component or is an interface.
@dylanh7244 I just tested this in Unity 5.6.0 and there were no errors. Did you modify the function?
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>();
Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren<T>()));
return results;
}
Update of best answer:
public static List<T> FindObjectsOfTypeAll()
{
List<T> results = new List<T>();
UnityEngine.Scene$$anonymous$$anagement.Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects().ToList().ForEach(g => results.AddRange(g.GetComponentsInChildren<T>(true)));
return results;
}
Even shorter:
public static List<T> FindObjectsOfTypeAll<T>()
{
return Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects()
.Select$$anonymous$$any(g => g.GetComponentsInChildren<T>(true))
.ToList();
}
Hi $$anonymous$$rXo can you give me an example of how I can use this, I tried to use the tried and true copy paste method, but it's giving me an error.
$$anonymous$$y code is below
List<GameObject> gameObjectList = FindObjectsOfTypeAll<GameObject>();
public static List<T> FindObjectsOfTypeAll<T>()
{
return Scene$$anonymous$$anager.GetActiveScene().GetRootGameObjects()
.Select$$anonymous$$any(g => g.GetComponentsInChildren<T>(true))
.ToList();
}
It compiles and runs fine, however when it does that. I get an exception thrown
"ArgumentException: GetComponent requires that the requested component 'GameObject' derives from $$anonymous$$onoBehaviour or Component or is an interface."
What is causing this error for me?
Thanks
Answer by Senhor de todo o Mal · Aug 19, 2011 at 08:55 AM
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 co$$anonymous$$g 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 co$$anonymous$$g 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..
$$anonymous$$ake 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?
Answer by MrLucid72 · Jul 09, 2017 at 05:16 AM
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.
Answer by mdj_unity · Sep 21, 2021 at 01:39 PM
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);