- Home /
 
how do I reference a clones of a instantiated game object and only the clones In a script?
In a script, how do I make something happen to the clones of the object and only the clones of the object. For example if I wanted to destroy all the clones of the object and not the original game object, how would I do that? Thanks :)
Answer by ShadyProductions · Jul 24, 2020 at 01:03 PM
When you instantiate an object (clone) it will also return the gameobject that you can store as a variable.
 public GameObject Original;
 private List<GameObject> _clones = new List<GameObject>();
 
 public void Start()
 {
     for (int i=0; i < 10; i++)
     {
         var clone = Instantiate(Original, new Vector3(0,0,0), Quaternion.identity);
         _clones.Add(clone);
     }
 
     // When you're done with them destroy all the clones
     foreach (var cloneObj in _clones)
         Destroy(cloneObj);
     _clones.Clear();
 }
 
              @ShadyProductions how would I destroy a clone after a certain amount of time(once the clone has spawned in, so the clone is destroyed after a certain amount of time once its spawned in). Thanks.
You can just set time delay in Destroy method
  Object.Destroy(gameObject, 2.0f);
 
                  Your answer
 
             Follow this Question
Related Questions
How to compare the positions of two or more instantiated gameobjects (clones) with different tags? 1 Answer
Make a game object in a scene into a prefab with script 1 Answer
extra GameObjects Spawning 2 Answers
How to delete objects of a parent and then instantiate a new one? 1 Answer
instantiated gameobjects(clone) don't behave like its original gameobject. 1 Answer