- Home /
Destroy all clones
When game is over, I want to destroy all clones.
But just destroy one of all clones.
No destroy all clones.
I write like this:
if (gameover==true) {
Destroy(GameObject.FindWithTag("clone"));
}
There are clones and tag name are also call "clone".
How can I do? Thanks!
Answer by aldonaletto · Aug 12, 2011 at 06:18 PM
FindWithTag returns only one object. You must use FindGameObjectsWithTag: it returns an array of objects with the specified tag - just sweep this array and destroy each element, like this:
if (gameover){ // you don't need to compare a boolean variable to true var clones = GameObject.FindGameObjectsWithTag ("clone"); for (var clone in clones){ Destroy(clone); } }
This is the first time I use FindGameObjectsWithTag. I know now, thank you :)
I know this post is old, but can you explain how to sweep the array in C#?
Answer by Eldho · Jul 25, 2015 at 03:56 PM
in C# its
var clones = GameObject.FindGameObjectsWithTag ("clone"); foreach (var clone in clones){ Destroy(clone); }
Answer by amit-chai · Nov 30, 2011 at 03:57 PM
Hi, I am using this script you wrote for some time, but unless there is something wrong in my script, I think that there is an issue. This removes all the taged objects except one. how can i remove ALL ? Tnx, Amit
It's my failed code. Later, I was successful using the following code:
var clones = GameObject.FindGameObjectsWithTag ("clone"); for (var clone in clones){ Destroy(clone); }
Your answer
Follow this Question
Related Questions
Destroy one prefab clone of set of clones. 2 Answers
How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer
Destroying the original object destroys all its clones? 0 Answers
How to delete instantiated GameObject 4 Answers
I want to only destroy 3 of the clone prefabs that appear how would I do this? 1 Answer