Unity 3D C# - Collosion Code Not working?

This is my code

using UnityEngine;
using System.Collections;

public class ActivateKill : MonoBehaviour {

	void OnCollisionEnter(Collision collision)
	{
		if(collision.gameObject.name == "Killer")
		{
			//Destroy(col.gameObject);
			Debug.Log("COLLISION!");
		}
	}

}

Not sure what the problem is. The script is applied to the first person controller with a capsule collider with a capsule mesh, the capsule then collides with the cube, it is meant to show in the log that It has collided. but when it collides nothing happens nut sure, any suggestions?

Ok! Here are the solutions.

There are two ways to achieve this:

  1. OnControllerColliderHit()

  2. OnTriggerEnter()

Method #1:

The function OnControllerColliderHit() is available for the controller.

Code with that is:

void OnControllerColliderHit (ControllerColliderHit collision) {
		if(collision.gameObject.name == "Killer"){
		 Debug.Log("COLLISION!");
		}
}

But this is fired not just on controller enters the collision but also on stay.

Method #2:

Make your collider for controller object as a trigger.

Code is:

   void OnTriggerEnter(Collider collider)
    {
	
       if(collider.gameObject.name == "Killer")
       {
         //Destroy(collision.gameObject);
         Debug.Log("COLLISION!");
       }
    }

This will be fired once only when the collision is entered and not on stay.

As you are destroying the game object on collision, I suggest method #2 with trigger. Also OnControllerColliderHit() will be called continuously if you are walking on a floor which is just not required in this case.

Cheers!

Add RigidBody to your first person controller and don’t forget to Freeze its position to X,Y,Z.

No, Your problem has no relation with any programming language (C#).
I think you need to check your “killer” object’s property.
Is your object having rigidbody or not?
And if your is kinematic is checked or not?
Go through this, and i think it will solve your problem.
Just let me know whether your problem is solved or not.