How to Instantiate from Prefab and not latest clone?

Beginner programmer-- I want to get a part of my UI instantiating a ‘slot’ that can be created as many times that it needs to be, but disappear (Destroy itself) after a few seconds.

    public Transform notifGrid;
    public GameObject notifSlot;
    public NotifSlotController notifSlotController;
   
    public void PopNotif(string notifMessage)
    {
        if (notifSlot != null)
        {
        notifSlot = Instantiate(notifSlot) as GameObject;
        notifSlot.transform.SetParent(notifGrid, false);
        notifSlotController.Init(notifMessage);
        notifSlot.gameObject.SetActive(true);

        Destroy(notifSlot, 10);
        }
    }

A separate script that handles mouse input calls that function to create a NotifSlot whenever you click something.

The problem I’m having is that the public ‘notifSlot’ reference, which is initially looking at the NotifSlot prefab in a folder, continually has its reference updated to instead look at the latest clone of the prefab, rather than the original one in my project folder that isn’t going anywhere. So as as the last clone gets destroyed, it stops creating new objects all together because the reference is now empty. It also has the extremely annoying side effect of stacking up the ‘(clone)’ on each GameObject’s name, giving me “NotifSlot (clone)(clone)(clone)…”

How can I get it creating instances of the prefab from the original prefab every time?

Just… stop doing that? Make a new variable instead.

    public GameObject notifSlot;
    public NotifSlotController notifSlotController;
 
    public void PopNotif(string notifMessage)
    {
        if (notifSlot != null)
        {
          GameObject newInstance = Instantiate(notifSlot) as GameObject;
          newInstance .transform.SetParent(notifGrid, false);
          notifSlotController.Init(newInstance);
          newInstance .gameObject.SetActive(true);
          Destroy(newInstance, 10);
        }
    }
2 Likes

That works perfectly, thank you! Didn’t occur to me that I could just create a new variable at run time.