- Home /
How to destroy all the items with one function?
How do I destroy all the items with a tag?
I have looked at things but I am not exactly sure what I am doing wrong or what I have to do.
They are "cubes" coming at you. and I want the game to restart and delete all the cubes comming at you. The cubes are tagged as badCubes
I tried using:
var badCubesarray[] = GameObject.FindGameObjectsWithTag ("badCubes"); for (var badCubes in badCubesarray[]) { Destroy("badCubes" in badCubesarray); }
or
var badCubesvar = GameObject.FindGameObjectsWithTag ("badCubes"); for (var badCubes in badCubesvar) { Destroy("badCubes" in allBadCubes); }
I understand the logic is to create an array variable, add all the objects of that tag to it, and then create a for all the objects with that tag to destroy. But what exactly do I have to write?
Answer by krotana · Aug 04, 2011 at 02:59 AM
after a little bit of headake, I made it work! that and I finished the game on time for an assignment
var array : GameObject[] = GameObject.FindGameObjectsWithTag("taggedCubes");
var item : GameObject;
for (item in array)
{
Destroy(item);
}
}
My game for you fellas to check on, the cubes disapear when the game is restarted.
http://raws.adc.rmit.edu.au/~s3281341/dim2_assignment1/heroCube.html
Well, you posted a comment as an answer and didn't accept/vote up the answer that helped you (I guess that would be $$anonymous$$e). *sigh* As an aside, the iterator in a for/in loop doesn't exist outside the loop, so the "var item: GameObject;" line of code is completely irrelevant and should be removed. In other words, you have two different variables called "item" and one of them is unused.
This code does work however, if you play my game with the given link, once you "restart" the game all the cubes disappear.
What the var is doing, for every "GameObject" one by one, delete.
How otherwise would you write the code?
A number of different sources helped me with this problem. But who really helped me is a programmer friend from my course. He explained to me step by step and sent me to the correct links and places to look at.
Answer by Eric5h5 · Aug 03, 2011 at 06:14 PM
"Destroy("badCubes" in badCubesarray);
" isn't correct syntax. It's just "`Destroy(badCubes);`".
(Since you seem to be new here: Please format code using the code button, don't post comments as answers, and remember to accept/vote up answers that help you. Thanks!)
And if you name the variable badCube
, the logic may be clearer to you.