How to Check if prefab has been instantiated in Unity using C# code


There are different ways to check if a prefab has been instantiated in Unity using C# code. 


Here are some possible steps and c# code example for each method:


  • Using PrefabUtility.IsPartOfAnyPrefab method:

  1. Import UnityEditor namespace at the top of your script .
  2. Assign the prefab object that you want to check to a variable (e.g. myPrefab) 1.
  3. Use PrefabUtility.IsPartOfAnyPrefab(myPrefab) to return true if myPrefab is part of any prefab instance .

An example code for this method is:


using UnityEngine;
using UnityEditor; //Import UnityEditor namespace

public class CheckPrefab : MonoBehaviour
{
    public GameObject myPrefab; //Assign the prefab object
    
    void Start()
    {
        //Check if myPrefab is part of any prefab instance
        bool isPartOfAnyPrefab = PrefabUtility.IsPartOfAnyPrefab(myPrefab);
        
        Debug.Log("Is myPrefab part of any prefab instance? " + isPartOfAnyPrefab);
    }
}

  • Using a custom event and delegate:


  1. Create a static class with a delegate and an event that takes an Object as a parameter (e.g. ExtensionMethods.SpawnAction and ExtensionMethods.OnSpawn) .
  2. Create a static method that uses Object.Instantiate to spawn an object and invokes the event with the new object as an argument (e.g. ExtensionMethods.SpawnObject) .
  3. Subscribe to the event in another script using += operator and define what you want to do when the event occurs (e.g. print a message).


An example code for this method is:


using UnityEngine;

//Create a static class with a delegate and an event
public static class ExtensionMethods
{
    public delegate void SpawnAction(Object newObj); //Define a delegate type that takes an Object as a parameter
    
    public static event SpawnAction OnSpawn; //Define an event of that delegate type
    
    //Create a static method that spawns an object using Object.Instantiate and invokes the event
    public static Object SpawnObject(Object obj, Vector3 position, Quaternion rotation)
    {
        Object newObj = Object.Instantiate(obj, position, rotation); //Spawn an object
        
        if(OnSpawn != null) //Check if there are any subscribers to the event
        {
            OnSpawn(newObj); //Invoke the event with the new object as an argument
        }
        
        return newObj; //Return the new object
    }
}

public class CheckSpawn : MonoBehaviour
{
    public GameObject myPrefab; //Assign the prefab object
    
    void Start()
    {
        ExtensionMethods.OnSpawn += HandleOnSpawn; //Subscribe to the OnSpawn event
        
        ExtensionMethods.SpawnObject(myPrefab, Vector3.zero, Quaternion.identity); //Call SpawnObject method with myPrefab as an argument
        
        ExtensionMethods.OnSpawn -= HandleOnSpawn; //Unsubscribe from the OnSpawn event
    }
    
    void HandleOnSpawn(Object newObj)
    {
        Debug.Log("A new object has been spawned: " + newObj.name); //Do something when OnSpawn event occurs (e.g. print a message)
    }
}


  • Using GameObject.Find or GameObject.FindWithTag methods:

  1. Assign a name or tag to your prefab object (e.g. “My Prefab”).
  2. Use GameObject.Find(“My Prefab”) or GameObject.FindWithTag(“My Prefab”) to return an array of all game objects with that name or tag in your scene .
  3. Check if the array is empty or not using Length property.

An example code for this method is:


using UnityEngine;

public class CheckFind : MonoBehaviour
{
    
    void Start()
    {
        GameObject[] prefabs = GameObject.FindGameObjectsWithTag("My Prefab"); //Find all game objects with tag "My Prefab"
        
        if(prefabs.Length > 0) //Check if there are any prefabs in your scene
        {
            Debug.Log("There are " + prefabs.Length + " prefabs in your scene.");
            
            foreach(GameObject prefab in prefabs)
            {
                Debug.Log(prefab.name); //Print each prefab name
            }
            
        }
        
        else 
        {
            Debug.Log("Your scene has no prefabs.");
            
        }
    } //Add a closing bracket here
}