- Home /
Changing Health Display GUI
Here's hoping that this isn't a duplicate.
TL;DR Background Here:
So, I'm doing a test for a game I'm helping to create. I'm testing every component of the game with things called "spaces", something my physics teacher coined. So, the game itself is complicated as hell; the best way for me to learn how to program it is do trial and error and test with a crappy version along the way, effectively getting an idea on how I will process the game when actual development starts next year or so.
What I'm doing, at the moment, is called the "Battlespace"; those with military knowledge should know about that. To put it simply, it is the culmination of all forms and systems of combat in the game, since it is very combat-centric. So far, in the last few days, I've learned a lot. However, that's probably because I have some minimal experience with programming.
TL;DR Background End
Anyways, I've run into a bit of an issue. I was thinking about how one would display enemy health within the GUI; at first, I thought "health bar". But, I don't like those at all, and our Creative Director agrees. I wanted a system where I could display the health of the enemy as a simple integer upon focus on them. Not only that, but I want it to be able to change.
Imagine if you created a calculator on a webpage, and using JavaScript you tell the calculator to do this, this, and this... the result of your actions are shown after you use it, being that the integer is changed right in front of you. I want to know if GUI Text will do that for me, or if there is something better than that.
I would prefer it to be in JavaScript, but use whatever is most comfortable as I could just parse the information I need from it :)
If any pictures/examples of this potential GUI are needed, please tell me. Thank you in advance.
Answer by Cherno · Nov 18, 2013 at 12:41 AM
What exactly do you want to know? From what I understand, you just want to display a simple number over a unit that corresponds to the unit's current health.
private var WorldNamePos : Vector3;
var Health : float = 100;
function OnGUI()
{
WorldNamePos = Camera.main.camera.WorldToScreenPoint(Vector3(transform.position.x, transform.position.y + 0.3, transform.position.z));
GUI.Label (Rect (WorldNamePos.x - 50, Screen.height - WorldNamePos.y - 64, 100, 50), Mathf.Round(Health) + "");
}
This is amazing.
So, that world name position is only used for this script (just assu$$anonymous$$g, as it's private). Why is it a vector, though? Is there some sort of acceleration or direction for the display? Just a question; feel free to vilify me if it is obvious.
EDIT: Ok, so apparently I don't have the correct version to use floats.
BCE0023: No appropriate version of 'UnityEngine.GUI.Label' for the argument list '(UnityEngine.Rect, float)' was found.
Any way to change my version.
You can make the WorldNamePos variable public if you like, I just set it to private to it doesn't show up in the Inspector (helps if there are a lot of variables).
It's a Vector3 because it has to be one :D Seriously, this Vector3 is the actual spot in the game world where the GUI.Label is drawn at (but of course since it's called from OnGUI(), it is not actually part of the game world. A Vector3 can just act as coordinate information. After all, that's how transform.position and so on works.
Note that you can also make the Health variable an int if you like, then you could get rid of the $$anonymous$$athf.Round. If just like to have things like Health a flaot so I can work with it more flexibly.
If you feel that the answer has helped you, please remember to mark it as appected.
Thank you for the help. Things have been fixed, had to set it to a string. Thank you very much.
Oh, I just saw your edit in the first comment. I forgot that GUI:Label HAS to have a reference to a string, even if it empty. The line has to be like this:
GUI.Label (Rect (WorldNamePos.x - 50, Screen.height - WorldNamePos.y - 64, 100, 50), $$anonymous$$athf.Round(Health) + "");
Your answer
![](https://koobas.hobune.stream/wayback/20220613122109im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Health script, save health in between scenes. PlayerPrefs. 2 Answers
How to reduce number of digits in coordinate points? 2 Answers
Text Mesh Pro - Unicodes 0 Answers
GUI Display in game cash help 1 Answer
2D GUI Group moving with players. 1 Answer