- Home /
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!
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 =)
Answer by DevonJavaScript · Sep 05, 2011 at 09:12 AM
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.
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!
Any time. Sometimes you just need an extra set of eyes. PS: playerHealth references the variable you already have in the other script.
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)); }
Your answer
Follow this Question
Related Questions
Restart with GUIText 2 Answers
Taking a hit 3 Answers
Can you make a GUI.Box follow a player's position?(SOLVED) 7 Answers
GUI SCREW UPS! 1 Answer
Deduct health on collision 2 Answers