- Home /
This question was
closed Apr 08, 2013 at 01:06 PM by
Graham-Dunnett for the following reason:
Problem is not reproducible or outdated
When (health = 0) Destroy (GameObject)
Hello i'm making a shooting game but when my players health reaches 0 i need the player to be destroyed and i dont know how to do this in C# heres my script:
using UnityEngine;
using System.Collections;
public class PlayerHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
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
You need to tell the community what does/doesn't work. Don't just throw script at us.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why does this do nothing (Health Script) 2 Answers
Instantiate with Prefabs 2 Answers
ScriptableObject problem 1 Answer
Creating a Asteroid health script 5 Answers