- Home /
Delete all prefabs
For some reason when I call Destroy(prefabName);
(where prefabName=GameObject.Find("prefabName");
) only the first instance is deleted - the rest remain. How do you delete all prefab's of a certain name?
Answer by Loius · Dec 26, 2010 at 03:00 PM
The best way is to store them in an array as you create them, but -
var o : GameObject = GameObject.Find(delname);
while ( o ) {
o.name = delname + "-deleted";
Destroy(o);
o = GameObject.Find(delname);
}
if you store them as an array as you create them, how do you delete them?
also... this does not work - this freezes the game though (you have to force a crash to exit)
I'd have to test it to be sure, but it seems possible that Find() might continue to return an object that's been destroyed until the current update cycle has completed. If it does, the above code would create an infinite loop. (I'm just guessing here though - Find() may not work that way.) In any case, an alternative would be to use tags and FindGameObjectsWithTag(). (I'm not aware of any Unity function that finds all game objects with a specified name.)
I believe that's what Find was doing - infinite loop in update. FindGameObjectsWithTag seems to do the trick ... For caching each instantiate into a global array, I'd worry about unnecessary memory usage -
Ow, my apologies. I should've remembered that. I'll fix the answer to at least not put you in an infinite loop, I hope.
Your answer
Follow this Question
Related Questions
Deleting one object, not all objects at once. 1 Answer
How to not have a function affect a prefab? 2 Answers
delete parent or send message to parent? 2 Answers
My game has been deleted 2 Answers
assign ui panel to a prefab object ? 2 Answers