how to predefine an order to destroy objects on Unity?
the game I am designing is a mix between a runner and a puzzle game. The goal is simple you have to hit the cubes of the player in order to get the same shape as the square in the arch. I would like to know how to destroy in an exact order of objects here is an example.
And here is the code in which I manage my collisions.
public class Collision : MonoBehaviour {
public GameObject destroyedParticle;
public int maxHealth;
public int currentHealth;
Material m_Material;
void Start()
{
m_Material = GetComponent<Renderer>().material;
currentHealth = maxHealth;
}
void OnMouseDown() //If the object has been clicked
{
if ((FindObjectOfType<Player>().ammoCount > 0) && (!FindObjectOfType<Player>().gameIsOver)) //If the player has ammo and the game is not over yet
{
currentHealth -= 1;
FindObjectOfType<Player>().ammoCount -= 1; //Reduces ammo
m_Material.color = Color.grey;
if (currentHealth <= 0)
{
FindObjectOfType<AudioManager>().HavocSound(); //Sound effect plays
Destroy(Instantiate(destroyedParticle, transform.position, Quaternion.identity), 1f); //Instantiates a particle and destroys it after x seconds
Destroy(gameObject); //Destroys the cube which has been clicked
//FindObjectOfType<Player>().ammoCount--; //Reduces ammo
}
}
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Obstacle")) //If gameobject collides with an obstacle, then game is over
{
//Game over functions
FindObjectOfType<AudioManager>().DeathSound();
Destroy(Instantiate(destroyedParticle, transform.position, Quaternion.identity), 1f);
GetComponent<Animation>().Play("CubeDeathAnim");
FindObjectOfType<Player>().gameIsOver = true;
FindObjectOfType<GameManager>().EndPanelActivation();
GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Animation>().Play("CameraDeathAnim");
}
}
}
I would like to make a well-defined order to destroy the objects. For example first destroy the cube1 and after the cube2.
Your answer
![](https://koobas.hobune.stream/wayback/20220613041925im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Creating an array of objects in the order they are in a folder? 0 Answers
Objects getting destroyed in unity test, but only sometimes in the phone app 0 Answers
Can you get a value from a object when destroyed? 2 Answers
Colliders have a different postion to wheels want to reset colliders to have new postion origin 0 Answers
Code still trying to access Object that has been destroyed. 1 Answer