Emeny death with box collider

I have built a setup that is supposed to kill the enemy when player jumps on their head, much like a goomba from mario. but the enemy doesn’t die when player jumps on the box collider, that is parented to the enemy. Any tips that might help make this work after parent var is named in the inspector?

#pragma strict

var Parent : GameObject;

function OnCollision (collider : Collider){

Destroy(Parent);
Destroy(gameObject);

}

try this

function OnCollision (collider : Collider)
{
collider.Destroy(Parent);
collider.Destroy(gameObject);
}

I have heard of OnCollisionEnter , Exit, and Stay. But never heard of OnCollision. Maybe I missed that one. Anyway, in your case, I recommend you use function OnCollisionEnter ( ) . Hope this helps.

var Parent : GameObject;

function OnCollisionEnter (){
    Destroy(Parent);
    Destroy(gameObject);
}

There are a few things wrong with your code:

  • OnCollision is not a predefined function and it will not work in your case. Use OnCollisionEnter instead
  • There is no reason to use pragma strict(but that does no difference anyway)
  • And do not feed the function parameters when you are not using them it is just adding extra calculation, you don’t want that

The above code should work.