x


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

more ▼

asked Jan 20 '11 at 04:32 PM

Padges gravatar image

Padges
88 24 29 33

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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();
more ▼

answered Jan 20 '11 at 04:52 PM

Thom Denick gravatar image

Thom Denick
1.9k 13 20 38

:( im not too good at c#, if my variable is PlayerHealth, would i just replace myHealth in your sample script?

Jan 20 '11 at 05:01 PM Padges

I think in JS, you don't need the .ToString()... Just: myText.text = myHealth;

Jan 20 '11 at 05:32 PM Thom Denick
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 20 '11 at 05:22 PM

GamezAtWork gravatar image

GamezAtWork
233 26 29 35

i cant do a label, because the unity engine gives me error "No appropriate version of blah blah bah for the argument list

Jan 20 '11 at 05:29 PM user-4565 (google)
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Jan 20 '11 at 05:56 PM

user-4565 (google) gravatar image

user-4565 (google)
23 9 10 18

Yeah sorry about that, I guess you do need to do to convert PlayerHealthDisplay into a string.

Just try: guiText.text = PlayerHealthDisplay.ToString();

Jan 20 '11 at 05:59 PM Thom Denick

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

Jan 20 '11 at 06:07 PM user-4565 (google)
(comments are locked)
10|3000 characters needed characters left

If you want it Updated you should keep it inside of a function Update(){}

more ▼

answered Jan 28 '11 at 01:15 AM

Unity user gravatar image

Unity user
17 4 5 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3815
x394
x109

asked: Jan 20 '11 at 04:32 PM

Seen: 7022 times

Last Updated: Jan 20 '11 at 04:32 PM