- Home /
Where is my HP Bar?
Start learning GUI. Trying to make HP bar. Some wrong?
using UnityEngine;
using System.Collections;
public class playerHP : MonoBehaviour {
public int maxHealth = 100; //Max HP
public int curHealth = 100; //Current HP
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void onGUI () {
GUI.Box(new Rect(10, 10, Screen.width / 2 / (maxHealth / curHealth), 10), " "); //Must show HP bar.. But I don't see it ;(
}
}
Comment
Answer by Eric5h5 · Aug 25, 2012 at 03:49 PM
Using only integers in division results in integer division. Use floats, or at least cast to float while dividing, so you can have float division instead.
Answer by Different · Aug 25, 2012 at 03:49 PM
I see that it's from BurgZerg Arcade. Maybe try original script? It works fine for me.
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)
maxHealth = 1;
healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
}
}
Your answer
Follow this Question
Related Questions
Object Colour Change 1 Answer
GUI box problem? 0 Answers
Health & HUD Script, not syncing 1 Answer
Custom GUI.Box function dilemma 1 Answer
Strange behaviour GUI.Box 1 Answer