T[] A list of all objects of the given type.
Obtains a list of all objects of type T.
This method can return any type of Unity object that is loaded, such as GameObjects, prefabs, materials, meshes, and textures. It also lists internal objects, therefore be careful with the way you handle the returned objects.
Unlike Object.FindObjectsOfType, this method also lists disabled objects.
The following example returns all GameObjects in the scene, in List<GameObject> format.
using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class ExampleScript : MonoBehaviour { List<GameObject> GetAllObjectsOnlyInScene() { List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]) { // EditorUtility.IsPersistent is true for objects saved to disk as assets (such as prefabs). // The leading ! keeps only in-scene objects and excludes those assets. if (!EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)) objectsInScene.Add(go); }
return objectsInScene; } }
The following example returns all persistent GameObjects in a scene.
using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class ExampleScript : MonoBehaviour { List<GameObject> GetNonSceneObjects() { List<GameObject> objectsInScene = new List<GameObject>();
foreach (GameObject go in Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[]) { // This is the same query as the previous example, but without the leading ! on IsPersistent. // It keeps only persistent GameObjects (assets such as prefabs) and excludes in-scene objects. if (EditorUtility.IsPersistent(go.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)) objectsInScene.Add(go); }
return objectsInScene; } }
| Parameter | Description |
|---|---|
| type | The type of object. |
Object[]
Returns any array of loaded objects of type type.
Obtains all objects of type type.
This method uses non-generic types and can return any type of Unity object that is loaded, such as GameObjects, prefabs, materials, meshes, and textures. It also lists internal objects, so be careful how you handle the returned objects.
Unlike Object.FindObjectsOfType this method also lists disabled objects.
Note: This method is very slow. Avoid using it every frame.
The following code example finds all GameObjects in the scene using non-generic methods:
using UnityEngine; using UnityEditor; using System.Collections.Generic;
public class ExampleScript : MonoBehaviour { List<UnityEngine.Object> GetSceneObjectsNonGeneric() { List<UnityEngine.Object> objectsInScene = new List<UnityEngine.Object>();
foreach (UnityEngine.Object go in Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)) as UnityEngine.Object[]) { GameObject cGO = go as GameObject; if (cGO != null && !EditorUtility.IsPersistent(cGO.transform.root.gameObject) && !(go.hideFlags == HideFlags.NotEditable || go.hideFlags == HideFlags.HideAndDontSave)) objectsInScene.Add(go); }
return objectsInScene; } }