Destroying an object in sequence with button press.

Hey, Im trying to figure out how to destroy 3 cubes in sequence with a button press. Cubes 1,2,3… the 3rd cube can’t be destroyed until 2 is destroyed, and 2 can’t be destroyed until 1 is destroyed. So far I have

using UnityEngine; using
System.Collections;

public class Hit : MonoBehaviour {
	
	public GameObject cube1;

public GameObject cube2;
public GameObject cube3;

	void Start () 
	{
		
	}
	

	void Update () 
	{
		if (Input.touchCount >0) 
		{
			RaycastHit hit;
			Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
			if (Physics.Raycast(ray, out hit))
				if (hit.collider.gameObject.tag =="new")
			{
				Destroy(cube1);
                          Destroy(cube2);
                          Destroy(cube3);
			}       
		}
	} 
}

I would drag the cube to be destroyed into the script but when I press the button they would all get destroyed.I’m fairly new at this so a push in the right direction would greatly help…thanks

You need to wait a little after each destroy, I’m guessing that’s the effect you are after. Something like this should work:

void Update () 
    {
        if (Input.touchCount >0) 
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            if (Physics.Raycast(ray, out hit))
                if (hit.collider.gameObject.tag =="new")
            {
                DestroyCubes(new List<GameObject>(){cube1, cube2, cube3});
            }       
        }
    } 

 IEnumerator DestroyCubes(List<GameObject> cubesToDestroy) 
 {
    foreach(var cubeToDestroy in cubesToDestroy)
    {
        Destroy(cubeToDestroy);
        yield return new WaitForSeconds(1);
    }
 }

Just be sure to add your cubes to the list sent as parameter in the order you want them destroyed. Also, be sure to add a bool that tells you if the game is in the process of destroying the cubes, so you don’t call the coroutine multiple times.