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 bibbis · Jul 25, 2017 at 06:13 PM · collision detectiondestroy object

How is it possible to destroy a GameObject twice?

I'm making a 2D game. Enemies drop "power" when they die. It took me a long time to figure out why enemies sometimes drop two "powers" instead of one. It's because I have an error in my collision logic, so sometimes my enemies get hit twice by one bullet.

When enemies get hit, they spawn a "power" object and then destroy their own gameObject. However, a second hit is still allowed to execute code, as well as destroy the gameObject again.

 float powerValue = 1;

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.gameObject.layer == LayerMask.NameToLayer("PlayerBullets"))
     {
         health -= collision.gameObject.GetComponent<BulletBasicCollision>().damage;
     }
     if (health <= 0)
         Die();
 }

 private void Die()
 {
     GameObject power = Instantiate(powerPrefab, transform.position, transform.rotation) as GameObject;
     power.GetComponent<Power>().SetValue(powerValue);

     Destroy(gameObject);
 }

By using break points and having the condition that it only triggers after 2 hits, I can see that the OnTriggerEnter2D function sometimes triggers twice, and the code executes all the way through the Die function. Which includes spawning two "power" objects and running the Destroy(gameObject) line twice. How is that possible?

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
Best Answer

Answer by Daemonhahn · Jul 25, 2017 at 06:19 PM

This is an error that has happened before and unfortunately the only way to control it is to use a bool to determine if the collision has happened and then ignore subsequent collisions.

something like:

collided = false;

OnTriggerEnter() { if(collided) return;

else collided = false

}

around your code

Comment
Add comment · Show 3 · 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 Daemonhahn · Jul 25, 2017 at 06:20 PM 0
Share

similar to http://answers.unity3d.com/questions/738991/ontriggerenter-being-called-multiple-times-in-succ.html

avatar image bibbis · Jul 25, 2017 at 09:07 PM 0
Share

I see. Thanks for your answer. Yes I ended up using a bool to prevent this.

Just to clarify, do you mean "else collided = true" ?

avatar image Daemonhahn bibbis · Jul 26, 2017 at 11:52 AM 0
Share

yes i did :)

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

68 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

Related Questions

Why are the objects (they are cows) randomly disappearing? 2 Answers

Why can't I destroy this object!? 3 Answers

How do I destroy a bullet when it hits something? 2 Answers

Destory() on Collision if Tag = Lazer [SOLVED] 1 Answer

Diablo 3 style melee combat 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