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?