- Home /
problem with displaying GUI
So I have a 3Dtext object that displays the health of my character. The objects code is as follows
//var PlayerHealthDisplay : float;
//function Update() { //FPSWalker.PlayerHealth = PlayerHealthDisplay; //GetComponent(TextMesh).text = PlayerHealthDisplay.ToString();
//} (ignore the "//", i was using them to see if my script was working.)
But as you know, my static var PlayerHealth, which this script receives its data from, will change through gameplay from receiving damage.
Now this is the issue: My Health will not update. I set a PRINT line of code in FPSWalker to update me when the health is changed. I found : 1. When my 3D text object is disabled, PlayerHealth works, and when disabled, it doesnt work. 2. Placing PlayerHealth as a static var, float var, and int var all allowed PlayerHealth to still work, but when referenced by the script, it does not work. 3. Player Health is set to start at 100, when the 3D object is activated, it stays at 0, but when deactivated, it starts properly at 100. 4. When the 3D text script is active, it makes the PlayerHealth script in FPsWalker stop working also.
Anyone have any clues as to why PlayerHealth is not transferred to the 3DText Object properly?
Answer by Bunny83 · Jan 22, 2011 at 04:48 AM
:D I think you have to swap this line:
FPSWalker.PlayerHealth = PlayerHealthDisplay;
It should look like:
PlayerHealthDisplay = FPSWalker.PlayerHealth;
otherwise you set your static PlayerHealth var to the value stored in this script and this value is 0 at the beginning.
edit
You don't even need your local var (PlayerHealthDisplay). You could use your static var directly:
GetComponent(TextMesh).text = FPSWalker.PlayerHealth.ToString();
and maybe you want to format the number a bit nicer:
GetComponent(TextMesh).text = FPSWalker.PlayerHealth.ToString("000.0");
that would create a string that always have 3 digits before and 1 digit after. Or use "000" without fractional part.
012.0
005.3
...
Your answer
![](https://koobas.hobune.stream/wayback/20220613100523im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
how to display the damage value from a mob 1 Answer
Restart with GUIText 2 Answers
health system problems 0 Answers
Health Bar Only For Falling Damage 2 Answers