12
votes

I have two 2D game objects. They each have a Box Collider 2D and a Rigid Body 2D which is not kinematic. When the game plays, one moves towards the other and collides with it.

However, I also have the following method in the moving GameObject:

void OnCollisionEnter(Collision collision) 
{
    print( "Collided with someone" );
}

The print statement never prints, so presumably the method is never called. Where am I going wrong?

1
Can you confirm that your class inherits from MonoBehaviour? Change Scripting reference to C# in the docs. A question about game-development can best be asked here: gamedev - Measuring
I haven't checked, but there is a OnCollisionEnter2D which you have to use. This has caught me out when I was converting a 2D game to their 2D framework... - T. Kiley
Ah! That was it, thanks T. Kiley =) - Jean Finley
Not to be that guy but could you accept then, thanks :) - T. Kiley

1 Answers

27
votes

Unity has replicated all of the physics methods for 2D with the word "2D" stuck onto the end! So for your example, it should be changed to:

void OnCollisionEnter2D(Collision2D collision)

And the same with basically any other 2D physics thing.