Bullet collision + hp

This is my script, that just want to subtract hp. Both the bullet object and the monster have colliders.

var hp = 5;
function OnCollisionEnter(hit : Collision)
{
// Bullets
if(hit.gameObject.tag == "Bullet")
{
hp--;
}
}

function Update()
{
if(hp < 1)
{
Destroy(gameObject);
}
}

What is wrong with it?

Edit: Bullet have this script:

function OnCollisionEnter(collision : Collision)
{
Destroy(destroybullet);
}

2 Answers

2

The code looks OK, try putting a print statement in OnCollisionEnter to make sure it is getting called.

that means that the function isn't being called.

Yeah try fiddling around with rigid bodies and 'is trigger'.

You may be getting tunneling. If the bullet is small and moving fast, it can pass through the collider in less than a frame in which case it won't be detected. Try slowing down the bullet. Also, I would assume your bullet has a rigidbody. Perhaps you can post the bullet instantiation script. The problem lie there and not in the collision script.