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 Jaitnium · Jul 17, 2020 at 07:37 AM · collision2d

Is the Physics engine overwriting the collision data?

I have a simple scenario where a ball (rigidbody2D + circlecollider2d) collides with two boxcolliders2d. Ball's OnCollisionEnter2D() is called twice (once per boxcollider). I need both collisions for my ball's logic, so I store the collision data for each collision, process it in FixedUpdate(), and then clear the data on each frame. The issue is that the first collision data is being overwritten by the second collision.

 //Variables in Ball class
 private ConcurrentBag<Collision2D> ballCollisions = new ConcurrentBag<Collision2D>();
 private String sanityString = ""; //For debugging

 // Update is called once per frame
 void FixedUpdate()
 {
     ProcessCollisionData();
 }

 private void ProcessCollisionData()
 {
     //Return if no collision data to process
     Debug.Log("Processing CollisionData, count: " + ballCollisions.Count);
     if(ballCollisions.Count == 0)
     {
         return;
     }

     Collision2D[] collisions = ballCollisions.ToArray();

     //Just output strings to determine if data is being overwritten
     Debug.Log(collisions[0].collider.gameObject.name);
     Debug.Log(collisions[0].otherCollider.gameObject.name);

     ContactPoint2D contactPoint2D = collisions[0].GetContact(0);
     Debug.Log("CP: " + contactPoint2D.point);
     Debug.Log("SanityString: " + sanityString);
     
     Debug.Log("Clearing ballCollisions");
     //Remove all data
     while(!ballCollisions.IsEmpty)
     {
         ballCollisions.TryTake(out Collision2D _);
     }
 }

This is the original version of "OnCollisionEnter2D":

 //Add collision(s) to be processed in the next frame
 private void OnCollisionEnter2D(Collision2D collision)
 {
     ballCollisions.Add(collision);
 }

Here is the modified version for testing:

 //Add collision(s) to be processed in the next frame
 private void OnCollisionEnter2D(Collision2D collision)
 {
     Debug.Log("OnCollisionEnter2D called");
     //First object has tag "Block", second object has tag "Wall", don't bother with Wall
     if(collision.gameObject.tag.Equals("Block"))
     {
         Debug.Log("Collided with something new!: " + collision.gameObject.name);
         ballCollisions.Add(collision);
         //Confirm it's actually added, this works as expected
         ballCollisions.TryPeek(out Collision2D temp);
         Debug.Log("ballCollision[0]: " + temp.gameObject.name);
         //See if string data will persist or be overwritten
         sanityString = collision.gameObject.name;
     }
     //This is only called on the second pass by the wall collision
     //At this point, the local data is already overwritten by the wall collision
     else if (ballCollisions.Count > 0)
     {
         Debug.Log("OnCollisionEnter2D, collision: " + collision.gameObject.name);
         ballCollisions.TryPeek(out Collision2D temp);
         Debug.Log("ballCollision[0]: " + temp.gameObject.name); //Outputs wall gameobject's name
     }
 }

I thought it was a race condition, but I modified my "ballCollisions" to something thread-safe and the issue persisted. I put in a conditional statement in OnCollisionEnter2D to ignore the second collision, but the collision data was STILL overwritten. Interestingly, the "sanityString" was NOT overwritten. The only way I can make sense of this is if the Physics engine is overwriting the data in memory, or if the collision data being passed is actually a reference. It feels like I'm missing something fundamental here. How can I avoid having the collision data overwritten?

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

Answer by xxmariofer · Jul 17, 2020 at 07:50 AM

i think your problem is a lot simpler that you think, i imagine that script is attached to the ball itself, right? because you said in the first line "//Variables in Ball class" if thats the case you need to mark the ballCollisions list static, since you are adding the collision data once for each ball instance

 private static ConcurrentBag<Collision2D> ballCollisions = new ConcurrentBag<Collision2D>();
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 Jaitnium · Jul 18, 2020 at 07:20 PM 0
Share

Thank you!

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

Disappearing platform problem - RigidBody2D and BoxCollider2D 0 Answers

Creating an infinite trail that can be collided with 2 Answers

Physics2D Mesh Collider? 1 Answer

Collision 2D doesnt work 0 Answers

problem Getting AI projectile to collide with player 0 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