3
votes

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.

2
use boxcollider2DCristiano Soleti
@CristianoSoleti Put that as answer.Programmer

2 Answers

5
votes

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.

0
votes

I know you already solved it, but I ran into the same problem and found your post. I was using the correct 2D collider, I was missing the Ridgedbody 2D component. It took me a while to find that so I wanted to add it here in case someone else comes across the same problem.