Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ludwu · Feb 23, 2018 at 05:10 PM · instantiatetransformprefabpositionclones

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);
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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);

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges