- Home /
getting every object in the scene
is there a way to get every object in the scene (including the inactive ones) through an editor/EditorWindow script.
I have been able to get all of the active Objects in the scene through
Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
but this will only ever return active GameObjects.
maybe if I could externally edit the selection.objects[] (storing current, selecting all, grabbing all the GameObjects in the array, and then resetting it to what it was), but this requires knowing how to get a hold of all the objects in the scene to add them to the array.
I think the better leading question is does anyone know how the engine gets all objects in the scene for say select all?
Answer by Jamora · Sep 28, 2013 at 06:28 PM
You can get every GameObject in the Scene by Resources.FindObjectsOfTypeAll(typeof(GameObject));
.
However, that returns each and every GameObject in the scene, including hidden GameObjects used by Unity, such as the SceneCamera and PreviewCameras ...
You can emulate the "Select All" behaviour with the following method:
private void FindAll(){
Object[] tempList = Resources.FindObjectsOfTypeAll(typeof(GameObject));
List<GameObject> realList = new List<GameObject>();
GameObject temp;
foreach(Object obj in tempList){
if(obj is GameObject){
temp = (GameObject)obj;
if(temp.hideFlags == HideFlags.None)
realList.Add((GameObject)obj);
}
}
Selection.objects = realList.ToArray();
}
I know this is old, but the solution is problematic for a few reasons. First, 'FindObjectsOfTypeAll' doesn't find all objects, only 'loaded' ones. Second, the above might return object in the Project but not in the scene - I kept seeing prefab blueprints (as well as instances) returned. I had better luck with jonc113 answer in this thread : https://answers.unity.com/questions/329395/how-to-get-all-gameobjects-in-scene.html
Answer by colinday · Dec 02, 2016 at 06:06 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();
}
This helped to get only root objects, Thanks @colinday , i don know y nobody upvoted it.
Your answer
Follow this Question
Related Questions
Auto-Selecting something else in Scene-View? 0 Answers
How can I set a selection-sphere in the editor 1 Answer
Problem with ExecuteInEditMode 0 Answers
Preventing editor deselect on MacOS 1 Answer