- Home /
How to loop through and remove Instantiate Gameobject clones
How do I loop through and remove Instantiate Gameobject clones?
What I have for making Gameobjects. But how do I loop through these and remove? var prefab: GameObject;
if ( prefab ) {
for(var i :int = 0; i == 0; i++){
Instantiate(prefab, CorrespondingGroundHit.point, Quaternion.identity );
}
}
Answer by robertbu · Feb 24, 2013 at 02:30 AM
First you need to find them. You can save them in an array when you create them. Or you can tag them all the same and use GameObject.FindGameObjectWithTag(). Or there are a few other ways to find game objects. Assuming FindGameObjectWithTag():
GameObject[] argo = GameObject.FindGameObjectsWithTag("YourTag");
foreach (GameObject go in argo) {
Destroy(go)
}
Answer by neogrant2 · Feb 24, 2013 at 04:29 PM
Hey thanks for the reply, much appreciated. Is your example in c# though? Just need to remove everything in the array, and disable/destroy the gameobjects (Prefab).
var Prefab: GameObject[];
if ( Prefab ) {
for(var i :int = 0; i == 0; i++){
Instantiate(Prefab[i], CorrespondingGroundHit.point, Quaternion.identity );
}
}
Do I need to do Destroy(Prefab[i]);
I'm confused by your question because Instantiate() creates object, not destroys them. Using a for loop to destroy the game objects in the prefab array it would be:
for (var i : int = 0; i < Prefab.Length; i++) {
Destroy(Prefab[i]);
}
Your answer
Follow this Question
Related Questions
Checking all elements of an array? 1 Answer
Puzzle + Grid Instantiate - Random 2 Answers
How to compare and detect overlapping gameObject Prefabs using maths 0 Answers
OverlapSphere for parallel arrays 1 Answer
IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers