Hello I need help with my health gui I have Everything I need for the Gui, I have it in the right place on the screen but its just Gui text. I was wondering if I could have it so I still keep the Gui text but have it decrease if the Player gets hit. Anything (scripts,Tutorials) would be really helpful. Thank you - ryan 123321
Do you have a health script on your player? Is there an 'HP' (or something) variable that you can query?
Answer by DGArtistsInc
Mar 07, 2012 at 02:32 AM
Yes it is possible. Make an int variable called health and set that to how much health you want. Then make a variable that is an empty gameobject called health text or something. then set your gui text to that variable.(Make sure all of this is in the same script BTW.) Then access the GUI text's text attribute and set it to the health variable. Then whenever your character gets hit subtract damage from your health variable. Heres an example.
var health = 100;
var guiText = gameObject;
guiText.text = health + "";
then make a function that subtracts damage from the health variable when the character is hit! If there are any errors or it is not what you want make sure to comment in reply!
Yeah there's an error. Spell Update properly. Also, why not use health.ToString() instead of relying on automatic type promotion?
My bad thanks for small detail improvement
thank you. I tryed making a script and it was simlar to this and it didnt work! Thank you so much.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
6 People are following this question.
Multiple Cars not working
Health and damage reciever
Damage/Healing over time