- Home /
Question by
n3cr0nzcwb · Mar 30, 2018 at 05:53 PM ·
if-statementsdestroy objectif
problem with simple gameover,gameover when hp reaches 0
I'm trying to get the character deleted when HP reaches zero but nothing happens, what's going on?
the code:
var GameOver: AudioClip;
function update ()
{
if(HealthVar.Health <= 0)
Destroy (gameObject);
AudioSource.PlayClipAtPoint(GameOver, transform.position);
}
Comment
Answer by Martin_Gonzalez · Mar 30, 2018 at 10:10 PM
Create a c# class and put this on any gameObject. Hit play and you will see how is deleted when health reaches 0
public class MyClass : MonoBehaviour{
public int health;
public Start()
{
health = 100;
}
public Update(){
health -= 0.5f;
if(health <= 0){
Destroy(gameObject);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612155711im_/https://answers.unity.com/themes/thub/images/avi.jpg)