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 /
avatar image
0
Question by afot2020 · Jul 19, 2020 at 08:38 AM · collision2ddestroy object

Collision detected but destroy not working

I suddenly have the weirdest bug which makes me going crazy. I use an invisible Scorezone. When the Player enters this Scorezone, he is scoring some points and the Scorezone destroys itself. Before that, a seconds Scorezone gets destroyed because you can only get one type of Score. So far so good.

This is the very simple code:

     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.tag == "PerfectScoreZone")
         {
             var controller = collision.gameObject.GetComponent<SecondScoreZoneController>();
             if (controller)
             {
                 Destroy(controller.SecondScoreZone);
             }
             Score();
             Destroy(collision.gameObject);
         }
 }


The collision gets perfectly detected. The Second Scorezone gets destroyed and the Score()-Method is called everytime. However, sometimes, (not every time which makes this even more difficult to diagnose), the scorezone itself does not get destroyed (last line of code), which makes the zone behave like a physical object moving the player.

And here is another part: when moving the player away from the scorezone and then colliding again, the destroy-method suddenly works.

I did not have this issue before. I somehow must have changed something causing this issue. I would say this issue occurs every 10th time, when the scorezone is touched.

Thank you for your help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by afot2020 · Jul 19, 2020 at 12:25 PM

UPDATE

Using Debug.Log I found out that collision.GetContacts(contacts)) returns 0 whenever the issue occurs. So the collision seems not to be detected. BUT: why is the if-Statement called after all? The collsion somehow IS detected...

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
avatar image
0

Answer by davidcox70 · Jul 19, 2020 at 03:51 PM

Sometimes I have found that hard-to-resolve bugs around colliders related to a collision being called multiple times - for example as a player skims a collision zone, effectively going in and out rapidly. The solution can be to add and extra condition to the trigger. For example;

      if (collision.gameObject.tag == "PerfectScoreZone" && !triggered)
     { 
     triggered=true;
 ``...

This stops a double trigger. This might not be your issue, but worth a shot. Remember to reset trigger for the next time you need it. DC

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 afot2020 · Jul 21, 2020 at 04:23 AM 0
Share

Thank you for your tip! What actually solved the issue was calling the Destroy-$$anonymous$$ethod at the beginning of the if-Statement

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

131 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

Related Questions

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

Documentation unclear about destroying the object 2 Answers

Why does Destroy(this.gameObject) destroys a prefab that got nothing to do with the script 0 Answers

Trying to apply force to an object 1 Answer

Colliders at wrong position after building the game? 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