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 13 days ago
Definition
- Type: Method
- Namespace: UnityEditor
- Assembly: UnityEditor
FindAllInstancesOfPrefab(GameObject)
Retrieves the root GameObjects for all instances of the Prefab asset with root found in all currently loaded scenes. If is not a valid Prefab asset root GameObject, an is thrown.
prefabRootprefabRootArgumentExceptionpublic static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot)
Parameters
The root GameObject of a Prefab asset.
Returns
Type | Description |
|---|---|
| GameObject[] | The root GameObjects for all instances of the Prefab asset with root |
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 found in all currently loaded scenes. If is not a valid Prefab asset root GameObject, an is thrown.
prefabRootprefabRootArgumentExceptionpublic static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot, Scene scene)
Parameters
The root GameObject of a Prefab asset.
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 |
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]); }}