Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


FindObjectsOfTypeAll

Obtains all objects of type type.
Read time 2 minutesLast updated 4 days ago

Definition

  • Type: Method
  • Namespace: UnityEngine
  • Assembly: UnityEngine.CoreModule

FindObjectsOfTypeAll(Type)

Obtains all objects of type
type
.
public static Object[] FindObjectsOfTypeAll(Type type)

Parameters

type

The type of object.

Returns

Type

Description

Object[]Returns any array of loaded objects of type
type
.

Remarks

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:

Examples

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; }}

FindObjectsOfTypeAll<T>()

Obtains a list of all objects of type
T
.
public static T[] FindObjectsOfTypeAll<T>() where T : Object

Returns

Type

Description

<T>[]A list of all objects of the given type.

Remarks

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; }}