Creating a GUI without assigning it a transform

I have a problem. I have a health variable(HEALTH), which has its own script. I want to be able to put the amount of health into a GUI text, but to not have to put

if(HEALTH==1)
{
instantiate gui;
}
if(HEALTH==2)
{
instantiate gui;
}

and so on. I want it to print the current HEALTH to a GUI text, and for it to destroy the old one. I do not know how to do this, however. Help would be appreciated.

Why would you destroy the old one? Just change the text of the current GUI object to reflect the health.

–Eric

Ok, but how would I change the old one without changing it in the editor?

I dunno if I’m missingsomething here but this seams really simple. Have the gui text equal the health variable and use .tostring()

My health is not a string, it’s an int, if this means anything. But would the code I put on the GUI text literally be
text = HEALTH;?

If HEALTH is your variable, yes that will work as it will do the ToString() automatically. You can explicitly call it as well, if you prefer:

text = HEALTH.ToString();

1 Way:

var health = 5;

function OnGUI(){
GUI.Label (Rect (10, 10, 200, 20), "health: " +health.ToString(), 25);
}

function Update()
{
if (Input.GetKeyDown(KeyCode.T)) health++;
}

(Untested, but should be fairly close to the mark).