# 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.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor](/engine/6000.3/script-reference/unityeditor.md)
* **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.

```csharp
public static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot)
```

### Parameters

**** (\[GameObject]\(/engine/6000.3/script-reference/unityengine/gameobject)): The root GameObject of a Prefab asset.

### Returns

| Type                                                                        | Description                                                                        |
| --------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| [GameObject\[\]](/engine/6000.3/script-reference/unityengine/gameobject.md) | The root GameObjects for all instances of the Prefab asset with root `prefabRoot`. |

### Remarks

FindAllInstancesOfPrefab will not return nested Prefab instances.

### Examples

```csharp
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.

```csharp
public static GameObject[] FindAllInstancesOfPrefab(GameObject prefabRoot, Scene scene)
```

### Parameters

**** (\[GameObject]\(/engine/6000.3/script-reference/unityengine/gameobject)): The root GameObject of a Prefab asset.**** (\[Scene]\(/engine/6000.3/script-reference/unityengine/scenemanagement/scene)): The scene to search for Prefab instances. The scene you specify must be valid and loaded.

### Returns

| Type                                                                        | Description                                                                        |
| --------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- |
| [GameObject\[\]](/engine/6000.3/script-reference/unityengine/gameobject.md) | The root GameObjects for all instances of the Prefab asset with root `prefabRoot`. |

### Remarks

FindAllInstancesOfPrefab will not return nested Prefab instances.

### Examples

```csharp
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]);
    }
}
```
