var health = 60;
var something : GameObject;
function OnCollisionEnter(thing : Collision) {
if (thing.gameObject.tag == "Bullet") {
health =- 1;
}
else
if (thing.gameObject.tag == "FlameBall") {
health =- 3;
}
else
if (thing.gameObject.tag == "Bomb") {
health =- 6;
}
}
function Update () {
if (health <= 0)
Destroy (something);
}
i shot like 20 bombs at the enemy and they were still alive and i could tell they hit because they bounced off
Your script has to be on the enemy (make sure that it is).
Try changing gameObject to collider so it says `thing.collider.tag`.
To decrease a value by its current you use `-= value` not `=- value`, what happens now is that you set the current value to a negative value immediately.
Try an output to the console by `Debug.Log(health.ToString());` after you've decreased the health to see what actually happens.
A case switch in this case would be cleaner:
switch (thing.collider.tag) {
case "Bullet":
health -= 1;
break;
}
To destroy a GameObject by its reference from a script attached to it you only need to do this: `Destroy(gameObject);`
Keep in mind that rigidbodies traveling to fast wont be calculated with precision and you might expect awkward behavior. For bullets you could use a raycast instead of letting the physics engine calculate a rigidbody, for bombs you could use an OverlapSphere and AddExplosionForce.
I think u should check the tags. Make sure the objects that u wish to have tag have the tag. Also, recheck the capital letter of the tag. It is very sensitive. Check if the tag is capitalized like one written in the script or not. Good luck