12

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?

4
  • 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
    – Measurity
    Dec 2, 2013 at 17:29
  • 4
    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
    Dec 2, 2013 at 17:33
  • Ah! That was it, thanks T. Kiley =) Dec 2, 2013 at 18:16
  • Not to be that guy but could you accept then, thanks :)
    – T. Kiley
    Dec 4, 2013 at 15:54

1 Answer 1

27

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.