x


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?

more ▼

asked Oct 08 '10 at 06:51 PM

Bumbaz gravatar image

Bumbaz
25 3 3 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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]);
}

Click here to go to the reference page for this method

more ▼

answered Oct 08 '10 at 07:19 PM

edwood_grant gravatar image

edwood_grant
188 2 3 12

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1671
x764
x25

asked: Oct 08 '10 at 06:51 PM

Seen: 1857 times

Last Updated: Oct 08 '10 at 06:51 PM