Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jun 12, 2015 at 07:52 AM by DiebeImDunkeln for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DiebeImDunkeln · Apr 14, 2015 at 08:38 AM · prefabdestroycoroutineinstance

Problem destroying cloned prefabs

I instantiate a lot bat prefab from within a coroutine. Each prefab has a touch detection which should destroy the clone on touch.

My problem now, is that all clones a destroyed at once, if I touch a prefab. And not only the one that got touched.

this is the script which generates the bat prefabs:

 public class BatGenerator : MonoBehaviour {
 
     public GameObject Bat;
     public GameObject CountDown;
     private GameObject Main;
 
     private int CountDownTimer;
     private bool GameDone;
     private bool BatGeneratorRunning;
     private int BatCounter;
 
     // Use this for initialization
     void Start () {
         GameDone = false;
         Main = GameObject.Find("Main");
         BatGeneratorRunning = false;
         BatCounter = 1;
     }
 
     void Update()
     {
         CountDownTimer = Convert.ToInt32(CountDown.GetComponent<Text>().text);
         
         if (Main.GetComponent<SchreiGameEvents>().GameRunning && !BatGeneratorRunning)
         {
             BatGeneratorRunning = true;
             Debug.Log("Start Coroutine");
             StartCoroutine("GenerateBat");
             
         }
 
         if (GameDone)
         {
             Application.LoadLevel("GameScene");
         }
 
     }
 
     IEnumerator GenerateBat()
     {
         CountDownTimer = Convert.ToInt32(CountDown.GetComponent<Text>().text);
 
         while (CountDownTimer > 0)
         {
             CountDownTimer = Convert.ToInt32(CountDown.GetComponent<Text>().text);
             Vector3 BatPos = new Vector3(UnityEngine.Random.Range(-6f, 6f), UnityEngine.Random.Range(-0.7f, 3.36f), 0);
             GameObject clone = (GameObject)Instantiate(Bat, BatPos, Quaternion.identity);
             clone.name = "Bat_" + BatCounter;
             Animator BatAnimator = clone.GetComponent<Animator>();
           
             BatAnimator.Play("BatFly");
             yield return new WaitForSeconds(0.6f);
             BatCounter++;
         }
         GameDone = true;
        
         yield return null;
         
     }
 }

And this is my AI script on each prefab to detect the touch event and destroy the clone:

 public class BatAI : MonoBehaviour
 {
     private GameObject BatHitCounter;
     private int killedBats;
     private bool alreadyHit;
     private Vector3 lastScale;
     public Animator BatAnimator;
    
 
     public AudioClip BatSwatter;
     private GameObject Main;
     private bool BatAnimationRunning;
     private RuntimePlatform platform;
 
 
     void Start()
     {
         BatHitCounter = GameObject.Find("batHitCounter");
         Main = GameObject.Find("Main");
         BatAnimationRunning = false;
         platform = Application.platform;
         alreadyHit = false;
     }
 
     // Update is called once per frame
     void Update () {
 
         if (Main.GetComponent<SchreiGameEvents>().GameRunning && !BatAnimationRunning)
         {
             BatAnimationRunning = true;
             this.BatAnimator.Play("BatFly");
         }
         if (Main.GetComponent<SchreiGameEvents>().GameRunning)
         { 
             if (this.transform.localScale.x >= 0.59f && !alreadyHit)
             {
                 
                 BatAnimator.SetTrigger("MaxSizeReached");
                 Destroy(this.gameObject);
                
             }
         }
         if (platform == RuntimePlatform.Android || platform == RuntimePlatform.IPhonePlayer)
         {
             if (Input.touchCount > 0)
             {
                 if (Input.GetTouch(0).phase == TouchPhase.Began)
                 {
                     checkTouch(Input.GetTouch(0).position);
                 }
             }
         }
         else if (platform == RuntimePlatform.WindowsEditor)
         {
             if (Input.GetMouseButtonDown(0))
             {
                 checkTouch(Input.mousePosition);
             }
         }
 
     }
 
     private void checkTouch(Vector3 pos)
     {
          Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
          Vector2 touchPos = new Vector2(wp.x, wp.y);
          Collider2D hit = Physics2D.OverlapPoint(touchPos);
      
          if(hit)
          {
              Debug.Log(hit.transform.gameObject.name);
              //hit.transform.gameObject.SendMessage('Clicked',0,SendMessageOptions.DontRequireReceiver);
              lastScale = this.transform.localScale;
              alreadyHit = true;
              Debug.Log("Hit");
              BatAnimator.SetTrigger("Hit");
              this.transform.localScale = lastScale;
              StartCoroutine(DestroyBat(hit));
          }
     }
     
     IEnumerator DestroyBat(Collider2D hit)
     {
         SoundManager.instance.PlaySingle(BatSwatter);
         killedBats = Convert.ToInt32(BatHitCounter.GetComponent<Text>().text);
         BatHitCounter.GetComponent<Text>().text = (killedBats+1).ToString();
         yield return new WaitForSeconds(0.4f);
         BatAnimator.SetTrigger("Dead");
 
         Destroy(this.gameObject);
     }
    
 }

  

I hope you can help me with that problem.

Thanks in advance Frank

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

  • Sort: 
avatar image
1
Best Answer

Answer by HarshadK · Apr 14, 2015 at 08:50 AM

Inside your

 private void checkTouch(Vector3 pos)

along with if(hit) you should also check if the hit is registered on that prefab only otherwise every prefab will note that a hit was made and irrespective on which prefab it was registered they will destroy themselves. You can change your if(hit) to something like:

 if(hit && hit == gameObject.GetComponent<Collider2D>())
 {
     // Your other stuff
 } 
Comment
Add comment · Show 1 · 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
avatar image DiebeImDunkeln · Apr 14, 2015 at 08:55 AM 0
Share

that saved my day. Thanks :)

Follow this Question

Answers Answers and Comments

18 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

Related Questions

How to refer to a collision between clones of the same prefab? C# 1 Answer

Destroy an instance of a prefab 1 Answer

How to destroy a game object within an instance of prefab 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

How do I instantiate a prefab B in same place prefab A? 1 Answer


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