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.
CloseFor 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.
Closetype | Type of the class to match while searching. |
Object[] An array of objects whose class is type or is derived from type.
Returns a list of all objects of Type type
.
This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal objects. Therefore be careful with the way you handle the returned objects.
Contrary to Object.FindObjectsOfType this function will also list disabled objects.
Note: This function is very slow and is not recommended to be used every frame.
using UnityEngine; using System.Collections;
public class ExampleScript : MonoBehaviour { // This script displays the number of allocated Unity objects by type. // This is useful for finding leaks. Knowing the type of object // (mesh, texture, sound clip, game object) that is getting leaked is // the first step. You could then print the names of all leaked assets of that type.
void Start() { print("All " + Resources.FindObjectsOfTypeAll(typeof(UnityEngine.Object)).Length); print("Textures " + Resources.FindObjectsOfTypeAll(typeof(Texture)).Length); print("AudioClips " + Resources.FindObjectsOfTypeAll(typeof(AudioClip)).Length); print("Meshes " + Resources.FindObjectsOfTypeAll(typeof(Mesh)).Length); print("Materials " + Resources.FindObjectsOfTypeAll(typeof(Material)).Length); print("GameObjects " + Resources.FindObjectsOfTypeAll(typeof(GameObject)).Length); print("Components " + Resources.FindObjectsOfTypeAll(typeof(Component)).Length); } }
using UnityEngine; using UnityEditor; using System.Collections.Generic;
public class ExampleScript : MonoBehaviour { // 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; } }
Returns a list of all objects of Type T
.
This function can return any type of Unity object that is loaded, including game objects, prefabs, materials, meshes, textures, etc. It will also list internal stuff, therefore please be extra careful the way you handle the returned objects.
Contrary to Object.FindObjectsOfType this function will also list disabled objects.
Please note that this function is very slow and is not recommended to be used every frame.
using UnityEngine; using UnityEditor; using System.Collections.Generic;
public class ExampleScript : MonoBehaviour { // This script displays the number of allocated Unity objects by type. // This is useful for finding leaks. Knowing the type of object // (mesh, texture, sound clip, game object) that is getting leaked is // the first step. You could then print the names of all leaked assets of that type.
void Start() { print("All " + Resources.FindObjectsOfTypeAll<UnityEngine.Object>().Length); print("Textures " + Resources.FindObjectsOfTypeAll<Texture>().Length); print("AudioClips " + Resources.FindObjectsOfTypeAll<AudioClip>().Length); print("Meshes " + Resources.FindObjectsOfTypeAll<Mesh>().Length); print("Materials " + Resources.FindObjectsOfTypeAll<Material>().Length); print("GameObjects " + Resources.FindObjectsOfTypeAll<GameObject>().Length); print("Components " + Resources.FindObjectsOfTypeAll<Component>().Length); } }
using UnityEngine; using UnityEditor; using System.Collections.Generic;
public class ExampleScript : MonoBehaviour { // 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; } }
Display the stored GameObject names:
using UnityEngine; using UnityEditor; using System.Collections.Generic;
public class ExampleScript : MonoBehaviour { // Find all of the GameObjects in the Resources folder // Add a GameObject called "Player" so at least 1 GameObject will be stored. // However, there are nearly twenty GameObjects.
void Start() { string s = ""; GameObject[] gameObjects; int count = 0;
GameObject player = new GameObject("Player"); gameObjects = Resources.FindObjectsOfTypeAll<GameObject>();
foreach (GameObject go in gameObjects) { s = s + go.name;
if ((count == 4)) { s = s + "\n"; count = 0; } else { s = s + ' '; count++; } }
Debug.Log(s);
// Prints: //Player SceneLight SceneLight SceneLight SceneCamera //GameObject InternalIdentityTransform torus cube cylinder //sphere PreviewMaterials Cylinder HandlesGO Cone //Cube Sphere Directional Light Main Camera } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thanks for helping to make the Unity documentation better!