|
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!
(comments are locked)
|
|
First off you have many options. 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)
|

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
Yeah, easy for most normal people however I struggle with the easiest of scripting, Thanks for answering though =)