- Home /
Health Bar Colour
Hey everyone, Im in the process of making a game for a uni assignment and think my health bar looks a bit ugly and needs some colour. I thought its as simple as adding another line of code but i must be mistaken or getting in wrong (I'm new to Unity and programming). In the code below I thought by adding the GUI.background colour line it would work. Any help would be greatly appreciated.
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(240, 555, healthBarLength, 20), curHealth + "/" + maxHealth);
GUI.backgroundColor = Color.red;
}
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);
}
}
Answer by ScroodgeM · Jul 30, 2012 at 05:53 AM
void OnGUI(){ GUI.color = Color.red; GUI.Box(new Rect(240, 555, healthBarLength, 20), curHealth + "/" + maxHealth); }
http://docs.unity3d.com/Documentation/ScriptReference/GUI-color.html http://docs.unity3d.com/Documentation/ScriptReference/GUI-contentColor.html http://docs.unity3d.com/Documentation/ScriptReference/GUI-backgroundColor.html
Thanks Scroodge this made the colour of the font in the bar red, but i was hoping to make the whole bar red ins$$anonymous$$d of the greyish tint. Any further help would be awesome.
GUI.color changes whole color, not just for font. contentColor and backgroundColor can set color separately for content and bg.
Ive added the GUI.color = Color.red; to my code and the background remains grey with red font. Any idea as too why its not changing the background.
There's no effect when i do GUI.backgroundColor = Color.red; The box stays default colour.
Your answer
Follow this Question
Related Questions
How to add texture in onGUI() 0 Answers
Health Help D: 2 Answers
Far Cry 3 Health Bar System 2 Answers
Non-static member Unity3d error 2 Answers
Enemy Health Bar 1 Answer