Documentation

Unity Engine


Unity Engine


FindAllInstancesOfPrefab

Retrieves the root GameObjects for all instances of the Prefab asset with root prefabRoot found in all currently loaded scenes. If prefabRoot is not a valid Prefab asset root GameObject, an ArgumentException is thrown.
Read time 2 minutesLast updated a month ago

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor

FindAllInstancesOfPrefab(GameObject)

Retrieves the root GameObjects for all instances of the Prefab asset with root
prefabRoot
found in all currently loaded scenes. If
prefabRoot
is not a valid Prefab asset root GameObject, an
ArgumentException
is thrown.
public static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot)

Parameters

prefabRoot

The root GameObject of a Prefab asset.

Returns

Type

Description

GameObject[]The root GameObjects for all instances of the Prefab asset with root
prefabRoot
.

Remarks

FindAllInstancesOfPrefab will not return nested Prefab instances.

Examples

using UnityEditor;using UnityEngine;using UnityEngine.SceneManagement;using NUnit.Framework;public class Example : MonoBehaviour{ public GameObject prefabAssetRoot; private void Start() { var expectedInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot); var instances = PrefabUtility.FindAllInstancesOfPrefab(prefabAssetRoot); Assert.AreEqual(1, instances.Length); Assert.AreEqual(expectedInstance, instances[0]); }}

FindAllInstancesOfPrefab(GameObject, Scene)

Retrieves the root GameObjects for all instances of the Prefab asset with root
prefabRoot
found in all currently loaded scenes. If
prefabRoot
is not a valid Prefab asset root GameObject, an
ArgumentException
is thrown.
public static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot, Scene scene)

Parameters

prefabRoot

The root GameObject of a Prefab asset.

scene

The scene to search for Prefab instances. The scene you specify must be valid and loaded.

Returns

Type

Description

GameObject[]The root GameObjects for all instances of the Prefab asset with root
prefabRoot
.

Remarks

FindAllInstancesOfPrefab will not return nested Prefab instances.

Examples

using UnityEditor;using UnityEngine;using UnityEngine.SceneManagement;using NUnit.Framework;public class Example : MonoBehaviour{ public GameObject prefabAssetRoot; private void Start() { var expectedInstance = (GameObject)PrefabUtility.InstantiatePrefab(prefabAssetRoot); var instances = PrefabUtility.FindAllInstancesOfPrefab(prefabAssetRoot); Assert.AreEqual(1, instances.Length); Assert.AreEqual(expectedInstance, instances[0]); }}