x


Gui Health Display Help!

Hi everyone! I'm really struggling with using the GUI to display my Players health, I know my players health script works because I can see it in the inspector going down when it takes damage but I have no idea how to turn that into a GUI so that when I actually export the game the person playing it can also see the health go down! The script i'm using for my player health is:

var playerHealth : float = 100; var difficulty : int = 5;

function OnTriggerStay(collision : Collider) { if(collision.CompareTag ("Enemy")) { playerHealth -= Time.deltaTime * difficulty; Debug.Log("Enemy is in contact."); } }

Any help is appreciated! I really want it to display as text rather then a health bar!

more ▼

asked Sep 05 '11 at 09:01 AM

rahra gravatar image

rahra
1 2 2 2

If you don't want an actual health bar, but you just want to display the number, then this is a 5-lines-of-code-task, and you can accomplish it by looking into the Unity GUI controls and the label, particularly.

This is the Unity GUI scripting guide:

http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html

Read it, study the code samples, and you will find that what you want is really easy to do. :P

Sep 05 '11 at 09:07 AM CHPedersen

Yeah, easy for most normal people however I struggle with the easiest of scripting, Thanks for answering though =)

Sep 05 '11 at 09:29 AM rahra
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

First off you have many options.

 var labelPos : Rect =  Rect(100,100,100,20);
  function OnGUI(){
         GUI.Label(labelPos, "HP:" + playerHealth); 
  }

This is probably one of the wisest decisions.
You can put it on an empty game object or your main camera and it will work.
more ▼

answered Sep 05 '11 at 09:12 AM

DevonJavaScript gravatar image

DevonJavaScript
56 12 20 20

Thanks heaps for that, but it just says that the player health is an unkown identifier, but you've given me an idea of what i'm supposed to be doing so thats a really huge help =) thanks!

Sep 05 '11 at 09:28 AM rahra

Any time. Sometimes you just need an extra set of eyes. PS: playerHealth references the variable you already have in the other script.

Sep 05 '11 at 09:39 AM DevonJavaScript

Got it working! I needed to add it onto my playerhealth script and add brackets to it! so now it reads:

var playerHealth : float = 100; var difficulty : int = 5; var labelPos : Rect = Rect(100,100,100,20);

function OnTriggerStay(collision : Collider) { if(collision.CompareTag ("Enemy")) { playerHealth -= Time.deltaTime * difficulty; Debug.Log("Enemy is in contact."); } }

function OnGUI(){ GUI.Label(labelPos, "HP:" + (playerHealth)); }

Sep 05 '11 at 09:42 AM rahra

Yeah. That was the intention.

Sep 05 '11 at 06:13 PM DevonJavaScript
(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:

x3686
x804
x383

asked: Sep 05 '11 at 09:01 AM

Seen: 1455 times

Last Updated: Sep 05 '11 at 06:13 PM