Search code examples
c#unity-game-enginecollision-detectioncollision

Unity: OnTriggerEnter2D not firing


I have a prefab for a red rectangle that has a BoxCollider2D set to trigger on it. I also have the player, controlled by the mouse, and it also has a BoxCollider2D (not set to trigger). When the player comes in contact, it should show a game over screen, but it does not do that.


Solution

  • In the image you provided in the comments, the Simulated property on your Rigidbody2D was turned off. Without this, the Rigidbody2D does not participate in the physics simulation, so the collider basically doesn't work.

    So just enable the Simulated property.

    Unity 2D physics uses Box2d. This Simulated property is referred to as activation in Box2d documentation

    You may wish a body to be created but not participate in collision or dynamics. This state is similar to sleeping except the body will not be woken by other bodies and the body's fixtures will not be placed in the broad-phase. This means the body will not participate in collisions, ray casts, etc.