Search code examples
c#2dcollision-detectionunity-game-engine

Unity5 OnTriggerEnter2D not called after collision (2D game)


I'm brand new to Unity3D 5 and I'm having issues with my first 2D game involving collision detection. My moving object is a ball and has Rigidbody2D and CircleCollider2D. My stationary "collider or trigger" is a BoxCollider and has a script attached. The OnTriggerEnter2D should be fired when the ball passes through the stationary box. I've also tried OnCollisionEnter2D but I'm fairly certain I should use OnTriggerEnter2D because my stationary box is marked as a trigger.

My code:

public class LoseCollider : MonoBehaviour {

public LevelManager levelManager;

void OnCollisionEnter2D(Collision2D collision)
{
    print("collide");
    levelManager.LoadLevel("Lose");
}

void OnTriggerEnter2D(Collider2D trigger)
{
    print("trigger");
    levelManager.LoadLevel("Lose");
}

void OnCollisionEnter(Collision collision)
{
    print("collide");
    levelManager.LoadLevel("Lose");
}

void OnTriggerEnter(Collider trigger)
{
    print("trigger");
    levelManager.LoadLevel("Lose");
}
}

As you can see I'm testing all variations and none are being called. Here are my Unity properties for my objects:

Ball

And

enter image description here

I'm sure I'm missing something simple if anyone can please point it out to me.


Solution

  • If you are going to to use OnTriggerEnter2D, you must also use a 2D Collider. You are currently using BoxCollider as shown in your second screenshot.

    Change that to BoxCollider2D and your Collision callback functions should be called.