How do I destroy an object on collision and at the same time trigger a new one to appear, one by one for many objects?

Hi! I would like to have an object appear in my scene, and once the player collided with it (‘picked it up’), it would disappear and a new object would appear on the scene. Once the player picked this one up, again, a new one would appear, and so on up until 4 or 5 objects. (It’s a bit like a search game and you can look for only one item at the time.)

I manually put down 4 spheres, and put these lines in my player’s movement script:

 void OnTriggerEnter(Collider other) 
{ 
if (other.gameObject.CompareTag("Sphere")) 
{ other.gameObject.SetActive(false); 
} 
}

So what I could achieve with this was that when the game starts, all objects are visible and the player can pick them up one-by-one.

However, I don’t know how to make the objects appear one by one. I understand how to deactivate a single object at Start(), and activate it when an event occurs, I just don’t understand how to do it dynamically when it comes to 4 objects. Could you please give me some advice? Thank you in advance.

Hi!

You probably want to store some list of the remaining spheres and activate one of those, then remove it from the list:

public List<GameObject> spheresToCollect;

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Sphere"))
    {
        other.gameObject.SetActive(false);

        if (spheresToCollect.Count > 0)
        {
            int randomIndex = Random.Range(0, spheresToCollect.Count);
            spheresToCollect[randomIndex].SetActive(true);
            spheresToCollect.RemoveAt(randomIndex);
        }
    }
}

Make sure to assign the spheres in the inspector.

Hope this helps!