- Home /
 
Destroy A Certain Instantiate On Tap Help?
So as the title states, what I am trying to do with these scripts is make it so that the game will spawn 0-5 objects and when a certain one of these instantiated objects are tapped, they are destroyed. My problem at the moment is that when I tap any of the objects that randomly spawn, only the original game object gets destroyed. Any suggestions would do wonders.
Script that spawns objects:
     public Transform prefab;
     private int randNum;
     private int radius;
 
     // Use this for initialization
     void Start ()
     {
         randNum = Random.Range(0, 5);
         radius = Random.Range(0, 5);
         for (int i = 0; i < randNum; i++)
         {
          Instantiate(prefab, Random.insideUnitSphere * radius + transform.position, transform.rotation);
         }
 
     }
 
               Script that allows game objects to be tapped and then destroyed:
     public Text countScore;
     private int score;
     public GameObject prefab;
 
     // Use this for initialization
     void Start () {
 
         score = 0;
         SetCountText();
 
     }
 
     // Update is called once per frame
     void Update()
     {
         if ((Input.touchCount > 0) && (Input.GetTouch(0).phase == TouchPhase.Began))
         {
             Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit raycastHit;
             if (Physics.Raycast(raycast, out raycastHit))
             {
                 Debug.Log("Something Hit");
                 Destroy(prefab);
 
             if (raycastHit.collider.name == "Button")
                 {
                     Debug.Log("Button clicked");
                 }
 
              Answer by JVene · Jul 18, 2018 at 03:00 AM
@Amanichai, if you create 5 objects, you have to destroy 5 objects. If you create 5 children of a parent object, you can delete the parent to delete the children.
To 'remember' the 5 objects (or whatever the quantity), you need to know what they are. You could "Find" them, or you could remember them in a container.
Try something like:
  public Transform prefab;
  private int randNum;
  private int radius;
 // a list for the objects
  public static List< GameObject > objects;
  // Use this for initialization
  void Start ()
  {
      // define storage for the list of objects
      objects = new List< GameObject >();
      randNum = Random.Range(0, 5);
      radius = Random.Range(0, 5);
      for (int i = 0; i < randNum; i++)
      {
       // remember what Instantiate returns as a GameObject
       GameObject o = Instantiate(prefab, Random.insideUnitSphere * radius + transform.position, transform.rotation) as GameObject;
       // add to the list of objects
       objects.Add( o );
      }
  }
 
               Then, when the time comes to remove all of them, something like:
 foreach( GameObject o in objects )
  {
   Destroy( o );
  }
 objects.clear(); // they're destroyed, so forget them.
 
              Your answer