Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


FindFirstObjectByType

Retrieves the first active loaded object of Type type.
Read time 5 minutesLast updated 4 days ago

Definition

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

FindFirstObjectByType<T>()

Retrieves the first active loaded object of Type
type
.
Warning
Deprecated. FindFirstObjectByType has been deprecated because it relies on instance ID ordering. Use FindAnyObjectByType instead, which does not depend on ordering.
public static T FindFirstObjectByType<T>() where T : Object

Returns

Type

Description

TReturns the first active loaded object that matches the specified type. If no object matches the specified type, returns null.

Remarks

Object.FindFirstObjectByType doesn't return Assets (for example meshes, textures, or prefabs), or inactive objects. It also doesn't return objects that have HideFlags.DontSave set.
Note: This function is very resource intensive. It's best practice to not use this function every frame and instead, in most cases, use the singleton pattern. Alternatively if you only need any instance of a matching object rather than the first one you can use the faster Object.FindAnyObjectByType

Examples

using UnityEngine;using System.Collections;// Search for the first active TextMesh and first active or inactive CanvasRenderer.// If found print the names, else print a message saying none were found.public class ExampleClass : MonoBehaviour{ void Start() { TextMesh texture = (TextMesh)FindFirstObjectByType(typeof(TextMesh)); if (texture) Debug.Log("TextMesh object found: " + texture.name); else Debug.Log("No TextMesh object could be found");// Include inactive objects in search for CanvasRenderer CanvasRenderer canvas = FindFirstObjectByType<CanvasRenderer>(FindObjectsInactive.Include); if (canvas) Debug.Log("CanvasRenderer object found: " + canvas.name); else Debug.Log("No CanvasRenderer object could be found"); }}

FindFirstObjectByType<T>(FindObjectsInactive)

Retrieves the first active loaded object of Type
type
.
Warning
Deprecated. FindFirstObjectByType has been deprecated because it relies on instance ID ordering. Use FindAnyObjectByType instead, which does not depend on ordering.
public static T FindFirstObjectByType<T>(FindObjectsInactive findObjectsInactive) where T : Object

Parameters

findObjectsInactive

Whether to include components attached to inactive GameObjects. If you don't specify this parameter, this function doesn't include inactive objects in the results.

Returns

Type

Description

TReturns the first active loaded object that matches the specified type. If no object matches the specified type, returns null.

Remarks

Object.FindFirstObjectByType doesn't return Assets (for example meshes, textures, or prefabs), or inactive objects. It also doesn't return objects that have HideFlags.DontSave set.
Note: This function is very resource intensive. It's best practice to not use this function every frame and instead, in most cases, use the singleton pattern. Alternatively if you only need any instance of a matching object rather than the first one you can use the faster Object.FindAnyObjectByType

Examples

using UnityEngine;using System.Collections;// Search for the first active TextMesh and first active or inactive CanvasRenderer.// If found print the names, else print a message saying none were found.public class ExampleClass : MonoBehaviour{ void Start() { TextMesh texture = (TextMesh)FindFirstObjectByType(typeof(TextMesh)); if (texture) Debug.Log("TextMesh object found: " + texture.name); else Debug.Log("No TextMesh object could be found");// Include inactive objects in search for CanvasRenderer CanvasRenderer canvas = FindFirstObjectByType<CanvasRenderer>(FindObjectsInactive.Include); if (canvas) Debug.Log("CanvasRenderer object found: " + canvas.name); else Debug.Log("No CanvasRenderer object could be found"); }}

FindFirstObjectByType(Type)

Retrieves the first active loaded object of Type
type
.
Warning
Deprecated. FindFirstObjectByType has been deprecated because it relies on instance ID ordering. Use FindAnyObjectByType instead, which does not depend on ordering.
public static Object FindFirstObjectByType(Type type)

Parameters

type

The type of object to find.

Returns

Type

Description

ObjectReturns the first active loaded object that matches the specified type. If no object matches the specified type, returns null.

Remarks

Object.FindFirstObjectByType doesn't return Assets (for example meshes, textures, or prefabs), or inactive objects. It also doesn't return objects that have HideFlags.DontSave set.
Note: This function is very resource intensive. It's best practice to not use this function every frame and instead, in most cases, use the singleton pattern. Alternatively if you only need any instance of a matching object rather than the first one you can use the faster Object.FindAnyObjectByType

Examples

using UnityEngine;using System.Collections;// Search for the first active TextMesh and first active or inactive CanvasRenderer.// If found print the names, else print a message saying none were found.public class ExampleClass : MonoBehaviour{ void Start() { TextMesh texture = (TextMesh)FindFirstObjectByType(typeof(TextMesh)); if (texture) Debug.Log("TextMesh object found: " + texture.name); else Debug.Log("No TextMesh object could be found");// Include inactive objects in search for CanvasRenderer CanvasRenderer canvas = FindFirstObjectByType<CanvasRenderer>(FindObjectsInactive.Include); if (canvas) Debug.Log("CanvasRenderer object found: " + canvas.name); else Debug.Log("No CanvasRenderer object could be found"); }}

FindFirstObjectByType(Type, FindObjectsInactive)

Retrieves the first active loaded object of Type
type
.
Warning
Deprecated. FindFirstObjectByType has been deprecated because it relies on instance ID ordering. Use FindAnyObjectByType instead, which does not depend on ordering.
public static Object FindFirstObjectByType(Type type, FindObjectsInactive findObjectsInactive)

Parameters

type

The type of object to find.

findObjectsInactive

Whether to include components attached to inactive GameObjects. If you don't specify this parameter, this function doesn't include inactive objects in the results.

Returns

Type

Description

ObjectReturns the first active loaded object that matches the specified type. If no object matches the specified type, returns null.

Remarks

Object.FindFirstObjectByType doesn't return Assets (for example meshes, textures, or prefabs), or inactive objects. It also doesn't return objects that have HideFlags.DontSave set.
Note: This function is very resource intensive. It's best practice to not use this function every frame and instead, in most cases, use the singleton pattern. Alternatively if you only need any instance of a matching object rather than the first one you can use the faster Object.FindAnyObjectByType

Examples

using UnityEngine;using System.Collections;// Search for the first active TextMesh and first active or inactive CanvasRenderer.// If found print the names, else print a message saying none were found.public class ExampleClass : MonoBehaviour{ void Start() { TextMesh texture = (TextMesh)FindFirstObjectByType(typeof(TextMesh)); if (texture) Debug.Log("TextMesh object found: " + texture.name); else Debug.Log("No TextMesh object could be found");// Include inactive objects in search for CanvasRenderer CanvasRenderer canvas = FindFirstObjectByType<CanvasRenderer>(FindObjectsInactive.Include); if (canvas) Debug.Log("CanvasRenderer object found: " + canvas.name); else Debug.Log("No CanvasRenderer object could be found"); }}