Destroying instantiated objects in order of appearance

I am trying to create a game where you need to tap (it’s an android game) dots in order of their appearance to destroy them.
I am not sure how to test to find the oldest instantiated object.

So far, the dot prefab is being instantiated on a timer. I test if the player has tapped on the dot by creating a ray cast. This I have managed to do, but at the moment you can tap on any dot to destroy it. I want you to have to destroy them in the order they appear in.

Create a list and add object to it when you instantiate them. The first item in the list is the oldest one. You can use a Queue as well which is the exact collection for this purpose, just happen to be slower than List from what I have seen but I could be wrong.

Just stack your dots to the array / list and then work with it. Below are few examples (code is not tested though).

Array example:

  1. Declare array in your class:

    	private GameObject[] dots;
    
  2. Initialize array with new dots:

    		private void Start()
    	{
    		dots = new GameObject[dotsCount];
    
    		for (int i = 0; i < dotsCount; i++)
    		{
    			GameObject newDot = Instantiate(myDotPrefab, ...);
    			dots *= newDot;*
    
  •  	}*
    
  •  }*
    

3. Destroy last array item:

  •  	public void DestroyLastDot()*
    
  •  {*
    
  •  	int dotsLeft = dots.Length;*
    
  •  	if (dotsLeft > 0)*
    
  •  	{*
    
  •  		int lastDotIndex = dotsLeft - 1;*
    
  •  		GameObject dotToDestroy = dots[lastDotIndex];*
    
  •  		Destroy(dotToDestroy);*
    
  •  		// will remove last item from array*
    
  •  		System.Array.Resize(ref dots, lastDotIndex);*
    
  •  	}*
    
  •  }*
    

List example:
1. Declare list in your class:

  •  	using System.Collections.Generic;*
    
  •  //...*
    
  •  private List<GameObject> dots;*
    

2. Initialize list with new dots:

  •  	private void Start()*
    
  •  {*
    
  •  	dots = new List<GameObject>();*
    
  •  	for (int i = 0; i < dotsCount; i++)*
    
  •  	{*
    
  •  		GameObject newDot = Instantiate(myDotPrefab, ...);*
    
  •  		dots.Add(newDot);*
    
  •  	}*
    
  •  }*
    

3. Destroy last list item:

  •  	public void DestroyLastDot()*
    
  •  {*
    
  •  	int dotsLeft = dots.Count;*
    
  •  	if (dotsLeft > 0)*
    
  •  	{*
    
  •  		int lastDotIndex = dotsLeft - 1;*
    
  •  		GameObject dotToDestroy = dots[lastDotIndex];*
    
  •  		Destroy(dotToDestroy);*
    
  •  		dots.RemoveAt(lastDotIndex);*
    
  •  	}*
    
  •  }*
    

You also may use Stack and Queue classes for this need, but they have some access restrictions and more advanced ways to achieve your target.