- Home /
How to follow multiple clones positions of an instantiate prefab having a velocity ?
Hi there ! Im new at coding, and i got a problem to destroy a clone gameobject from a prefab via input at a pricised position. Here is what i want : Im on a 2D scene, and 7 Prefabs are called randomly every 1 seconds and are positioned at a precise vertical. They automatically move to the left of the screen. Each prefabs get a unique controller button and are destroyed when the input is called. My problem is that for now, when i put the y button (for example), all clones of my prefabs are destroy. What i want is to detect the lefter precise prefab clone position, and destroy it. Example : clones of prefab 1, 2, 3, 4, 5, 6, 7 are instanciated and are moving to the left ; the lefter is one of the prefab 3 clones : then destroy it.
Here is my code :
 using System.Collections;
 using UnityEngine;
 
 public class NotesScript : MonoBehaviour
 {
     public GameObject[] HazardNotes;
     public float spawnWait;
 
     void Start()
     {
         StartCoroutine(SpawnNotes());
     }
 
     void Update()
     {
         GameObject Anote = GameObject.Find("QuarterAnote");
         GameObject Bnote = GameObject.Find("QuarterBnote");
         GameObject Cnote = GameObject.Find("QuarterCnote");
         GameObject Dnote = GameObject.Find("QuarterDnote");
         GameObject Enote = GameObject.Find("QuarterEnote");
         GameObject Fnote = GameObject.Find("QuarterFnote");
         GameObject Gnote = GameObject.Find("QuarterGnote");
 
         bool X_isAxisInUse = false;
         bool Y_isAxisInUse = false;
 
         if (Input.GetAxisRaw("dpadAxisH") != 0)
         {
             if (X_isAxisInUse == false)
             {
                 if (Input.GetAxisRaw("dpadAxisH") == +1)
                 {
                     Destroy (Anote);
                 }
             }
         }
         else if (Input.GetButton("yButton"))
         {
             Destroy(Bnote);
         }
         else if (Input.GetButton("aButton"))
             {
                 Destroy(Cnote);
             }
         else if (Input.GetAxisRaw("dpadAxisV") != 0)
         {
             if (Y_isAxisInUse == false)
             {
                 if (Input.GetAxisRaw("dpadAxisV") == +1)
                 {
                     Destroy(Dnote);
                 }
                 else if (Input.GetAxisRaw("dpadAxisV") == -1)
                 {
                     Destroy(Fnote);
                 }
                 Y_isAxisInUse = true;
             }
         }
         else if (Input.GetButton("xButton"))
         {
             Destroy(Enote);
         }
         else if (Input.GetButton("bButton"))
         {
             Destroy(Gnote);
         }
     }
 
     IEnumerator SpawnNotes()
     {
         while (true)
         {
             GameObject hazard = HazardNotes[Random.Range(0, HazardNotes.Length)];
             var clone = Instantiate(hazard);
             clone.name = clone.name.Replace ("(Clone)","");
             yield return new WaitForSeconds(spawnWait);
         }
     }
 }
Answer by dishant27 · Feb 24, 2018 at 06:47 PM
Attach the following code on the prefab: 
 public class ExampleClass : MonoBehaviour { public string mKey;
 void Update()
 {
       if (Input.GetKey(mKey))
      {
             Destroy(this);
      }
 }
}
And assign the value of mKey when instantiating it:
 GameObject hazard = HazardNotes[Random.Range(0, HazardNotes.Length)];
 var clone = Instantiate(hazard);
 clone.name = clone.name.Replace ("(Clone)","");
 var.GetComponent<ExampleClass>().mKey = "Up";     //whichever key you want, can use string array to store keyName
 yield return new WaitForSeconds(spawnWait);
Your answer
 
 
             Follow this Question
Related Questions
GameObject position and localPosition not changing in hiearchy, only in script. 0 Answers
Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer
problem whit Instantiate Prefabs position. 0 Answers
Why prefab revert does not work for position and rotation? 1 Answer
Enemy only chasing pre-fab's original location, not instance's 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                