- Home /
How do you get a GUI text to display a var like health?
How do you get a GUI text to display a var like health?
Answer by Justin Warner · Dec 14, 2010 at 05:13 AM
var currentHealth = 100; var maxHealth = 100; var healthPercent = 100; var damageTaken;
function Update() { healthPercent = healthUpdate()/maxHealth; //Add if statements for the GUI Textures to appear on screen...
} function healthUpdate(damageTaken) { //Do the broadcast message to this function. currentHealth = currentHealth-damageTaken; return currentHealth; }
Read up on broadcast message here: http://unity3d.com/support/documentation/ScriptReference/GameObject.BroadcastMessage.html
Did this off top of my head, some might be wrong, I think it's good though...
Answer by _Petroz · Dec 14, 2010 at 10:38 AM
You can display text on screen using GUI.Label
http://unity3d.com/support/documentation/ScriptReference/GUI.Label.html
Javascript:
function OnGUI () {
GUI.Label (Rect (10, 10, 100, 20), "Hello World!");
}
Answer by doomprodigy · Dec 14, 2010 at 12:59 PM
public float health = 100f;
void Update () { if (health <= 0) { removePlayer(); } else if (health <= 99) { print("health is now: "+health); GameObject.Find("g_Health").guiText.text = ""+health; }
That works as it is similar to one that I use in one of my games I half finished. everytime your health is deducted by a monster/ enemy void update checks to see if it is below 99 and if it is it renders the new number of HP you have. If your health <= 0 it calls removePlayer () which controlls dying.
Peace,
Actually, your last piece of code says if health is over 100, add another hundred to it. This will quickly result in it being over 9000.
Yeah I know, I just realised and was on my way to edit it. Looks like you beat me to it.
http://dragonball.wikia.com/wiki/It%27s_Over_9000!
Its a phenomena that got started when the english translator screwed up for Dragon Ball Z. Some guy on youtube made a video and it became a phenomena. Whenever someone says it, it's annoying because it is completely pointless.
Answer by ina · Dec 14, 2010 at 11:03 AM
1) Expose the variable public var health:int
in the script tracking health - call it GameLogic.js
2) Create a GUIText - GUI.Label, etc.. Put a script HealthDisplay in it, but use an exposed public variable named txt
for your string
3) In the Update()
event of GameLogic, put something like this: GetComponent(HealthDisplay).txt = health;
Your answer
![](https://koobas.hobune.stream/wayback/20220613090949im_/https://answers.unity.com/themes/thub/images/avi.jpg)