Version: Unity 6.5 (6000.5)
LanguageEnglish
  • C#

Resources.FindObjectsOfTypeAll

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static T[] FindObjectsOfTypeAll();

Returns

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

Description

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

Declaration

public static Object[] FindObjectsOfTypeAll(Type type);

Parameters

Parameter Description
type The type of object.

Returns

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

Description

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