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?

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:

Did this off top of my head, some might be wrong, I think it's good though...

You can display text on screen using GUI.Label


function OnGUI () {
    GUI.Label (Rect (10, 10, 100, 20), "Hello World!");

public float health = 100f;

void Update () {
if (health <= 0)
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.


1) Expose the variable `public var health:int` in the script tracking health - call it GameLogic.js

  1. Create a GUIText - GUI.Label, etc… Put a script HealthDisplay in it, but use an exposed public variable named txt for your string

  2. In the Update() event of GameLogic, put something like this: GetComponent(HealthDisplay).txt = health;