- Home /
Destroy multiple instances of an object
Hi! I am kind of new to Unity so please be patient.
I have a script that Instantiates a crate every two seconds, and that's fine. In my game, when you touch the ground, you lose. I want every instance of the crate to be destroyed when you lose. I imagine it is something that has to do with arrays and tags. I haven't found a solution yet though. How can I do this?
Answer by edwood_grant · Oct 08, 2010 at 07:19 PM
There is this function:
GameObject[] objects = GameObject.FindGameObjectsWithTag("YourTagHere");
You can use it to get an array of GameObjects that share the same tag. Then you can iterate the array and delete them.
for (int i=0; i<objects.Length; i++){
Destroy(objects[i]);
}
Your answer
Follow this Question
Related Questions
restricting clone number 2 Answers
Instantiate prefabs just before it comes into view 2 Answers
Instantiate with Prefabs 2 Answers
Destroy last placed object and put a new one? 1 Answer
Network.Destroy and collision; destroying correct object. 1 Answer