How can i alternate between objects?

Hello everyone. I’m making an avatar editor for my game. The character has 3 hat options and i want to swap from one to another WITHOUT disabling them because as far as i know, objects consume memory whether they are enabled or disabled so what i want to do is to instantiate the hats i have in an array and whenever the user swaps, the last hat is destroyed and the next one is instantiated and so on.

I have an array with the 3 hats prefabs stored inside. How would be a script that allows me to do change hats whenever i click in a button? As i mentioned, not disabling their visibility but instantiating/destroying the prefabs and if i reach the third hat option, on the next click i’ll have the first one visible and so.

Hope anyone can help me!

You can move through the array by incrementing a value. However, destroying and instantiating is a much heftier process than disabling and enabling. But, if you want to do it your way, then here’s a way to cycle through the array.

public GameObject[] Hats;
public GameObject Player;

GameObject LastHat;
int index = 0;

public void InstantiateNextHat()
{
     GameObject hat = Instantiate(Hats[index], Player.transform);
     
     if (LastHat != null)
          Destroy(LastHat);

     index++;
     LastHat = hat;

     // If on hat 3, go back to hat 1
     if (index == Hats.Length)
          index = 0;
}

@JuanC44