i made this script so when my bullet hit an enemy it destroy him. when my bullet hit him the damage goes down but when the hp reach 0 or - it does not destroy the object and i dont know why. can you guys help me out with this???
here is my script :
var hitPoints = 100;
var bulletDamage = 100;
function OnCollisionEnter(coll:Collision){
if(coll.gameObject.tag=="bullet")
{ hitPoints -= bulletDamage; }
}
function update (){
if (hitPoints <=0){
Destroy(GameObject);
}
}
2 Answers
2
A function called "update" will never run unless specifically called. You're probably looking for "Update", however that's not really the way to code this, because it's a waste of CPU time to check hitpoints every frame. Just put the hitpoints check in the OnCollisionEnter function so it runs only when necessary.
Make sure your bullet has a tag called bullet. I took your code you replied to Eric5h5 and it works just fine as long the other objects tag is bullet.
When my bullet hit him the damage goes down but when the hp reach 0 or - it does not destroy the object and i dont know why.
You must have changed the tag or something since, because it runs just fine.
For reference; this is the code (your code) I used.
var hitPoints = 100;
var bulletDamage = 100;
function OnCollisionEnter(coll : Collision)
{
if(coll.gameObject.tag == "bullet")
{
hitPoints -= bulletDamage;
}
if (hitPoints <= 0)
{
Destroy(gameObject);
}
}
i did what you told me but the object is still not destroy var hitPoints = 100; var bulletDamage = 100; function OnCollisionEnter(coll:Collision){ if(coll.gameObject.tag=="bullet") { hitPoints -= bulletDamage; } if (hitPoints <=0){ Destroy(gameObject); } }
– anon98876923