x


destroy all objects with a certain tag?

Hello. How can I destroy all objects with a certain tag?

I tried to do something like this, but It doesnt work.

var Enemies  : GameObject[];
    Enemies  = GameObject.FindGameObjectsWithTag("Enemy");
var exceptions : GameObject[];


function OnTriggerEnter (other : Collider) {
    if (other.gameObject.CompareTag ("Enemy")) {
       Stats.Life -=1;
       for (var GameObject in Enemies) 
            {
            Destroy(GameObject);
            }

EDIT -Updated the script, still doesnt destroy all of the objects with that tag.

more ▼

asked Nov 28 '10 at 10:23 PM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

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

2 answers: sort voted first

You just need to iterate through the array elements instead of trying to access the array itself.

So, instead of

Destroy (Enemies);

you will need to use

for (var enemy in Enemies) {
    Destroy(enemy);
}
more ▼

answered Nov 29 '10 at 07:26 AM

xeophin gravatar image

xeophin
357 8 11 23

Thanks, but it still doesnt destroy all of the objects with that tag, just that one specific object.

Nov 30 '10 at 05:52 AM Tyler 2

Hm, looking at your script you posted, I wonder whether the script is thrown off by your use of GameObject in the for loop. This would refer to a class instead of a variable but I could be wrong. The script does not throw any errors?

Nov 30 '10 at 10:29 AM xeophin
(comments are locked)
10|3000 characters needed characters left

This is also in answer to your similar question here.

You can acquire an array of references to all the active game objects in the scene with a specified tag using GameObject.FindGameObjectsWithTag(). Once you have this array, you can check its length to see how many such objects there are.

You can also perform operations on the list of game objects as needed (for example, destroy them all using Destroy(), activate or deactivate them, enable/disable specific components, etc.).

more ▼

answered Nov 28 '10 at 10:40 PM

Jesse Anders gravatar image

Jesse Anders
7.3k 7 17 48

I tried to implement something like that (or atleast something that sounds like that) in my edited question, But I am completely lost.

Nov 29 '10 at 06:40 AM Tyler 2
(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:

x1095
x326

asked: Nov 28 '10 at 10:23 PM

Seen: 5176 times

Last Updated: Nov 30 '10 at 05:52 AM