Why isn't my Death Script Working?

I’m having trouble figuring out why my code isn’t working to kill my player.
Here is my code:

var deathClip : AudioClip;
function OnCollisionEnter(collisioninfo : Collision) {
	if (collisioninfo.gameObject.CompareTag("Enemy")) {

This is simply saved under “DeathScript”, and it is placed on the player. The enemies are tagged as “Enemy” and continually follow the player. Both the player and the enemies have the BoxCollider component enabled. There is also one other script on the player, “Movement”, if that may be a problem. Any insight into the problem would be great.

I believe I have fixed the problem now. The function was never being called, as it never sensed a collision (I’m not sure why though). I made both the player object and the enemies all have the RigidBody component, which then made the code work.

So, to clarify, the function was never called when both objects had the Collider Component alone, and worked after adding a RigidBody component. If anyone could explain to me why, that would be awesome.

I have never seen ‘CompareTag’ used in a collision conditional statement and I haven’t seen ‘this.gameObject’ used. Your problem might be that A) the if statement is not receiving a boolean (true/false), or it does not recognize ‘this.gameObject’. I would change the if statement to this:

if (collisioninfo.gameObject.tag == "Enemy")

I would also change ‘this.gameObject’ to just ‘gameObject’.

Also check to see that the “Enemy” tag exists on the enemies. This makes a world of difference.

Your script is fine; either it’s not registering a collision at all, or there is no “Enemy” tag. Do some basic debugging like putting

print (collisioninfo.gameObject.name)

as the first line in the OnCollisionEnter function, just to see what, if anything, is happening.