Is it possible for OnCollisionEnter to be called twice within the same frame ?, according to this thread - OnTriggerEnter being called twice, OnTriggerEnter can be called twice within the same frame, can OnCollisionEnter be called twice aswell ? If so, would I be right in assuming that -
- Each time OnCollisionEnter is being called a new thread is created.
- Could I stop OnCollisionEnter being called twice within one frame by making it a synchronized function ?
- Can I make syncrhonized functions ?
- Can I stop this from happening by using OnCollisionExit like the link above suggests
I had similar issues with the OnTriggerEnter() being called twice, but I refused to believe this was a ‘bug’ I have to live with and continued to investigate. It turned out in my case that one of my colliders accidently had 2 collision components.
OnCollisionEnter() as others already suggested in the comments is completely synchronous (as is most of Unity, certainly on script level) and I would investigate why you are getting 2 calls instead of trying a “work-around” like using OnCollisionExit().
Possible reasons are:
- Having 2 actual colliders in one object (this can be in the scripted object OR the object it collides with)
- If your physics simulation becomes jittery, it is conceivable that during the physics FixedUpdate() iterations (which can happen several times for each “Frame”) an object would go in-out-in of another object, thus receiving 2 OnCollisionEnter() messages in a single frame could be possible and appropriate. You can test this by implementing OnCollisionExit() and seeing if you get a call there in the same frame as well.
If you investigated and ruled out these cases, try to create a simple repro. If you succeed file a bug
But again, I doubt such an obvious fundemental bug would persist for so long in PhysX. You can say a lot of things about PhysX but it’s been around for long enough to not have these issues …
FYI, OnCollisionEnter() is called once per collider in the hierarchy of that game object. So yes, it can be called twice in the same frame.