- Home /
How to make a GUI or HUD
I know there are tons of threads on here about making a gui but none of them seem to be working for me. I am trying to use a GUI.Label and make a "PlayerHealth" variable visible to the player as a number, not a healthbar. i.e. it shows 100 when at max and you get hit it falls down to 94. Is there any way to link a interger variable into a GUI.Label script?
thanks in advance
Answer by Thom Denick · Jan 20, 2011 at 04:52 PM
I think what you are looking for is not GUI.Label, but a GUIText
http://unity3d.com/support/documentation/ScriptReference/GUIText.html
When you have the Text in place, you can simply create a reference to it in your script, and change it's value by changing .text with your script.
So in C# just:
GUIText myText;
int myHeath = 100;
myText.text = myHealth.ToString();
:( im not too good at c#, if my variable is PlayerHealth, would i just replace myHealth in your sample script?
I think in JS, you don't need the .ToString()... Just: myText.text = myHealth;
Answer by GamezAtWork · Jan 20, 2011 at 05:22 PM
Actually... Can't you just do a OnGUI script?
Like...
function OnGUI(){
GUI.Label(0,0,200,200,PlayerHealth);
}
On the object that contains PlayerHealth? http://unity3d.com/support/documentation/ScriptReference/GUI.Label.html
If you're talking about using a GUIText though, like Smorpheus said above... It would be kinda like
theGuiText.text = PlayerHealth.ToString();
As he has done so above, with theGUiText referring to the GUIText you created, and PlayerHealth being the float/int variable you created.
i cant do a label, because the unity engine gives me error "No appropriate version of blah blah bah for the argument list
Answer by user-4565 (google) · Jan 20, 2011 at 05:56 PM
ok so, this is my new code
var PlayerHealthDisplay : int;
FPSWalker.PlayerHealth = PlayerHealthDisplay;
guiText.text = PlayerHealthDisplay;
but the console says "Cannot convert integer into String".
Now what?
Yeah sorry about that, I guess you do need to do to convert PlayerHealthDisplay into a string.
Just try: guiText.text = PlayerHealthDisplay.ToString();
ah i got it, it works perfectly :D
now i just need to find how to keep the health updated all the time lol thank you guys so much
Answer by Unity user · Jan 28, 2011 at 01:15 AM
If you want it Updated you should keep it inside of a function Update(){}