- Home /
Checking to see if there are no more of a certain object in the scene?
So I have a script, which I will only share a small part of, but basically when this function is called I want it to delete all the objects with the tag "ground" and then once those are deleted, I want it to debug "done deletin' shiz". Here's the function:
void DestroyLevel(){
GameObject[] dungeons = GameObject.FindGameObjectsWithTag("ground");
foreach (GameObject col in dungeons){
Destroy (col);
GameObject dungeon = col;
if (dungeon != null){
Debug.Log ("Still deletin' " + dungeon);
}
else {
Debug.Log ("Done deletin' shiz");
}
}
}
When I do this, I just get "still deletin' _" a bunch of times but I never reach "Done deletin' shiz" which is my goal. Any idea what is wrong with this script, I feel like I have tried everything but maybe I'm being an idiot. Any help is appreciated.
Answer by robertbu · Jan 21, 2014 at 04:01 AM
You have the right idea, but there is an issue here. You don't get a foreach cycle where 'col' will be null:
void DestroyLevel() {
GameObject[] dungeons = GameObject.FindGameObjectsWithTag("ground");
foreach (GameObject col in dungeons) {
Destroy (col);
Debug.Log("Still deletin' " + dungen);
}
Debug.Log("Done deletin' shiz");
}
I had tried this before, but calling another function in the place of "Done deletin' shiz" didn't work. But now I realize the error must be in the other function, so thank you.