# OnCollisionEnter2D not being called

I'm fairly new to the world of Unity, and am adding collision detection for the first time through RigidBody2D (as this game is 2D). First of all, here is some relevant information:

• Unity Version: 4.2.5f
• Operating System: Windows 8.1 Update 1

# The Problem

I have two objects that could collide, let's call one Bullet and one Enemy. Both Bullet and Enemy have RigidBody2D components attached to them, as well as the following method in an attached script:

void OnCollisionEnter2D(Collision2D collision){
print("Hit!");
}


However, nothing is printed to the console (I have checked that print works in other situations).

# The Question

What is going wrong here? I must be missing something simple. I have checked that Physics 2D has collisions enabled between layers, I have tried the 3D equivalent to this and still no joy.

You need to add a BoxCollider2D component to your GameObjects.

http://docs.unity3d.com/ScriptReference/BoxCollider2D.html

From the Unity docs for OnCollisionEnter2D

Sent when an incoming collider makes contact with this object's collider

A Rigidbody2D just tells a GameObject how to interact with the physics engine. It doesn't provide a collider for collision.

From the Unity docs for RigidBody2D

By adding the appropriate collider component, the sprite will also respond to collisions with other sprites.

• Ah thank you, I must have misread something as I thought RigidBody2D was an alternative rather than supplement! I'll mark this as a solution for now as it makes perfect sense to me, although I'm unable to test at present. Thanks again! – OMGtechy Jul 25 '14 at 18:52
• Confirmed to work (I'd also missed off the script for Enemy). Thank you! – OMGtechy Jul 26 '14 at 16:16

The problem could be either:

1. You've not marked your moving rigidbody with the right collision detection method. You need continious dynamic rather than discrete if you are moving your object with speed.

2. Are both rigidbodies marked as kinematic? If so the event won't call.

• 1) There is no continious dynamic option, but there is continious so I tried that. Sadly it didn't make any difference, and the objects are not travelling so fast that I'd think they'd require it, but it was worth a try! 2) Neither are kinematic. Thank you for your efforts so far :) – OMGtechy Jul 25 '14 at 15:48
• Are either colliders marked as triggers and at the right size? And do the colliders actually collide, i.e does one bounce off the other? – Lucas Jul 25 '14 at 15:50
• I cannot see a trigger option anywhere, and they do not bounce off each other either (although once this would be desired behavior when the implementation is feature complete as I'd like both objects to be destroyed) – OMGtechy Jul 25 '14 at 15:52
• Would it help if I posted an image with all the components attached to the object (alongside their settings)? :) – OMGtechy Jul 25 '14 at 15:58
• Yes, that would help :) Also, try decreasing the speed of the bullet. It's possible it's traveling so fast that there is no collision occurring between the objects (are they actually touching?). – Cooper Jul 25 '14 at 16:50