- Home /
How to get all GameObjects in Scene?
I need to get every single game object in the current scene in a GameObject[] using C#... Any ideas? Thanks in advance! :)
Answer by justinl · Oct 08, 2012 at 04:16 PM
You could try either of these:
GameObject.FindObjectsOfType(typeof(MonoBehaviour)); //returns Object[]
GameObject.FindGameObjectsWithTag("Untagged"); //returns GameObject[]
GameObject go = (GameObject) GameObject.FindObjectsOfType(GameObject);
I strongly advice against that solution. Because in my case, it got Unity editor to the state where it freezes
Well, your scene must be heavy or broken. Because these functions are not harmful.
But I agree these functions are not particularly efficient.
@$$anonymous$$ikilo, my scene is not that big actually. What can be broken about it?
@pd491 Consider these functions to be very dumb. The problem might be somewhere else. Ever tried an other scene?
Answer by jonc113 · Mar 29, 2013 at 12:17 AM
I have found that FindObjectOfType will return all kinds of weird things that are not in the scene. "activeInHeirachy" solves the problem:
GameObject[] allObjects = UnityEngine.Object.FindObjectsOfType<GameObject>() ;
foreach(object go in allObjects)
if (go.activeInHierarchy)
print(thisObject+" is an active object") ;
Looks fine to me. Probably an extra unnecessary pair of brackets in the if statement but it's fine imho.
Looks like it should be foreach(GameObject go in allObjects)
so that you can safely call activeInHierarchy
UPDATE: It looks like in the current version of Unity (4.6) FindObjectsOfType() will return only active objects in the scene.
So you no longer need to check the activeInHierarchy flag on each object.
Here's the docs: http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
Yeah, that is not always true, the definition of ActiveInHierarchy is :
This lets you know if a gameObject is active in the game. That is the case if its GameObject.activeSelf property is enabled, as well as that of all it's parents.
which means that a gameObject could be in the Scene but not enabled and it would not handle that case.
In my case, I used Resources.FindObjectsOfTypeAll<GameObject>():
combined with
if (!go.scene.isLoaded)
continue;
in my go loop and it worked like a charm.
Yeah, that is not always true, the definition of ActiveInHierarchy is :
This lets you know if a gameObject is active in the game. That is the case if its GameObject.activeSelf property is enabled, as well as that of all it's parents.
which means that a gameObject could be in the Scene but not enabled and it would not handle that case.
In my case, I used Resources.FindObjectsOfTypeAll<GameObject>():
combined with
if (!go.scene.isLoaded)
continue;
in my go loop and it worked like a charm.
Answer by colinday · Dec 02, 2016 at 06:01 PM
// get root objects in scene
List<GameObject> rootObjects = new List<GameObject>();
Scene scene = SceneManager.GetActiveScene();
scene.GetRootGameObjects( rootObjects );
// iterate root objects and do something
for (int i = 0; i < rootObjects.Count; ++i)
{
GameObject gameObject = rootObjects[ i ];
gameObject.DoSomething();
}
Unfortunately this way does not work well. It does not handle disabled GameObject.
@$$anonymous$$ikilo, actually that's not correct ... it does handle inactive game objects, inactive game objects are returned from GetRootGameObjects() as well as active game objects. I just tested this myself in the debugger on Unity 5.5.0f3.
@colinday I am sorry, I pointed the wrong thing. It handles inactive GameObject. BUT, it does not handle GameObject with the state DontDestroyOnLoad on them.
Using Unity 5.4.0F3.
@$$anonymous$$ikilo, sure enough you're right on that one ... which to me clearly seems like a bug. I've submitted it to Unity through the bug reporter, hopefully they will include those objects in the call in a future update.
@colinday $$anonymous$$aybe, since these objects are not in a scene anymore, we need to look for them somewhere else, I am not sure it's a bug, but we need to figure it out where to find them.
Edit: I did more tests, these objects seem to be kind of orphans. Which is pretty bad.
Answer by small-U · Nov 21, 2013 at 12:10 PM
C# code:
Transform[] hinges =GameObject.FindObjectsOfType(typeof(Transform)) as Transform[];
javaScript Code:
var hinges :Transform[] = FindObjectsOfType(Transform) as Transform[];
Answer by idbrii · Jan 29, 2018 at 07:19 PM
The Unity documentation for Resources.FindObjectsOfTypeAll has this example:
// This script finds all the objects in scene, excluding prefabs:
List<GameObject> GetAllObjectsInScene()
{
List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[])
{
if (go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)
continue;
if (!EditorUtility.IsPersistent(go.transform.root.gameObject))
continue;
objectsInScene.Add(go);
}
return objectsInScene;
}