Unity 5 OnCollisionEnter() not working

Hello,

I have searched both these forums and Google for a solution to my issue. It seems to be fairly common for people to have issues with OnCollisionEnter(), so I hate having to repost, but I have not found any of the other solutions to be helpful.

So I have two objects, a “Footman” and a “Wall”. Both have 2D Box Colliders with triggers disabled. Both have RigidBody2D attached, with only the wall having Kinematic checked. The following code is attached to my wall gameobject;

using UnityEngine;
using System.Collections;

public class CollisionDetection : MonoBehaviour {

public int hitPoints;

// Use this for initialization
void Start () 
{
	hitPoints = 100;
}

// Update is called once per frame
void Update ()
{
	if(hitPoints == 0)
	{
		Destroy (this.gameObject);
	}
}

void OnCollisionEnter(Collision col)
{
	Debug.Log ("Collision!");

	if(col.gameObject.tag == "Footman")
	{
		hitPoints =- 50; 
	}
}

}

I see the objects collide with each other, and the “Footman” gets stopped at the wall, but the debug statement never executes. I am either doing something really stupid, or something is broken.

Thanks a lot for any help!

Hey there :smiley:
I had that problem too… Its really simple:

void OnCollisionEnter2D () {}

or with the GameObject:

void OnCollisionEnter2D (Collision2D col) {}

Hope I could help you :slight_smile:

-Simple_zomb

2D physics? OnCollisionEnter2D is the right method for that, with Collision2D parameter.

You’re doing something really stupid…

void OnCollisionEnter2D() {}

You just need attach script to the same object, whose need detects the collision.

You need to put the Debug.log() inside the if statement and it needs to be OnCollisionEnter2D() !!