Hi!
I need to write a function that destroys
any object in the scene with a specific tag attached.
This code:
function destroyxyz () {
var tobedestroyed = GameObject.FindGameObjectsWithTag (“xyz”);
for (var go : GameObject in tobedestroyed) {Destroy (go);}
}
… adapted from a script example, does nothing noticeable.
Does anyone have an idea?
Thanx
Jens
it returns a list so you need to put the result in an array:
var tobedestroyed : GameObject [ ];
from here:
http://unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
EDIT: actually to be honest from that link i’m not sure what’s happening in the top part of the script i didn’t know it could be done that way (and it looks like that’s what you were trying) - maybe someone else can help with that but the array method works ; )
When you do it that way, the variable is automatically typed as an array, so you don’t actually have to specify “GameObject[ ]”. (Remember that nice type inference.
)
I’m not really sure why Jens’ code isn’t working. Should be fine, unless you’ve maybe entered the tag wrong (don’t forget about case sensitivity) or else the objects aren’t actually assigned the tag for some reason.
–Eric
ahh… good to know. thanks eric ; )
with the variable defined as an array, it works fine.
var tobedestroyed : GameObject [ ];
Thanks for the help!
Jens :o :lol:
That’s weird, here’s some code I wrote yesterday:
var ships = GameObject.FindGameObjectsWithTag("Ship");
for (var ship in ships) {
Destroy(ship);
}
And it works… But hey, whatever. 
–Eric
var tobedestroyed : GameObject[];
tobedestroyed = GameObject.FindGameObjectsWithTag ("Robot");
for (var go : GameObject in tobedestroyed) {Destroy (go);}
That’s my actual code.
Your’s is shorter and easier to read
(and it works in my script, too.)
I think I will use your’s, it’s better.

Jens