Question by
dadedas · Feb 05, 2018 at 04:41 PM ·
destroy objectdestroygameobject
how can i destroy the object when life get 0?,how can i destroy a object when life get 0?
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
void Start () {
healthBarLength = Screen.width / 2;
}
void Update () {
AddjustCurrentHealth(0);
}
void OnGUI(){
GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
}
public void AddjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 0)
curHealth = 0;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(maxHealth < 1)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612152003im_/https://answers.unity.com/themes/thub/images/avi.jpg)