Add Clones of GameObject to List(array)
Hey, i have a script which spawns objects on random positions. I want to press a button to destroy one of the objects that are currently colliding with a specific object(b). I tried to put the spawned objects in a list if they are colliding with object(b). than i could use 
 var i = Random.Range(0, objects.Count); 
 Destroy(objects[i]);
 BUT the clones arent in the list but the "parent" Object...so theres just one , not all the clones. And so all objects (that are colliding) get destroyed... i dont know what to do please help :) 
Thats the spawn script which spanws my objects. I am testing the code only on the first (red) one
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class spawner : $$anonymous$$onoBehaviour {
 public GameObject red_ball, blue_ball, green_ball, $$anonymous$$l_ball, purple_ball;
 public float spawnRate = 2f;
 float nextSpawn = 0f;
 int whatToSpawn;
 int nextRedNumber;
 // Update is called once per frame
 void Update()
 {
 Vector2 spawnPos = new Vector2(Random.Range(-2.6f, 2.6f), 6.5f);
     if (Time.time > nextSpawn)
     {
         whatToSpawn = Random.Range (1,6);
         switch (whatToSpawn) {
             case 1:
                 GameObject red = Instantiate(red_ball, spawnPos, Quaternion.identity) as GameObject;
                 red.name = "redball " + nextRedNumber;
                 nextRedNumber += 1;
                 break;
             case 2:
                 Instantiate(blue_ball, spawnPos, Quaternion.identity);
                 break;
             case 3:
                 Instantiate(green_ball, spawnPos, Quaternion.identity);
                 break;
             case 4:
                 Instantiate($$anonymous$$l_ball, spawnPos, Quaternion.identity);
                 break;
             case 5:
                 Instantiate(purple_ball, spawnPos, Quaternion.identity);
                 break;
         }
         nextSpawn = Time.time + spawnRate;
     }
     if(spawnRate >= 0.1f) {
         spawnRate -= 0.0001f;
     }
     
 }
}
and thats my script of my red object
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class col_red : $$anonymous$$onoBehaviour { private bool onTrigger = false; public buttonPressed button;
     void OnCollisionEnter2D(Collision2D collision)
     {
         {
             if (collision.gameObject.tag == "laser")
             {
                 {
                     //Destroy(this.gameObject);
                     onTrigger = true;
                 redballs.Add(this.gameObject);
                 Debug.Log(redballs.ToString());
                 }
             }
         }
     }
     void Update()
     {
     Debug.Log(redballs.Count);
        
     
     var i = Random.Range(0, redballs.Count);
     if (button.pointerDown)
         {
         if (onTrigger == true)
             {
             Destroy(redballs[i]);
             
             pointer = false;
         }
         }
     }
}
who has the colliding script? the parent?
Theres no colliding script. In the script of the ball i check if it is colliding with the GameObject with the tag "laser"
Your answer
 
 
             Follow this Question
Related Questions
destroying instantiated groups of objects 1 Answer
Destroy GameObject in list then instantiate GameObject not working 1 Answer
Problem with Destroy function with instantiated Prefabs 1 Answer
Instantiate and then destroy a prefab in a list. C# 1 Answer
Issue iterating through gameObjects deleting and addding some 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                