- Home /
GUI Label problem
So Right now, I have my energy level displayed through a GUI text. Heres my code
static var MAX_ENERGY = 100.0;
var energy = MAX_ENERGY;
var energyLimit = MAX_ENERGY;
var energyDisplay: GUIText;
function Update(){
if(energy >= MAX_ENERGY){
energyDisplay.text = "100";
} else {
energyDisplay.text = energy.ToString().Substring(0,2);
}
}
But now I want it updated in a GUI Label
expample:
GUI.Label(Rect(0, 0, 0, 0), "Energy Display");
any idea how to do this?
Answer by Bradley DM · Jun 03, 2011 at 09:44 PM
You simple want something like the following? if i am following you right
function OnGUI() {
GUI.Label(Rect(0, 0, 200, 64), "Energy Display" +energy);
}
Well see, in my original script, it only showed the whole numbers. I have an energy regeneration that goes Time.deltaTime. So as its increasing its showing all the tiny variables to. I just want to see the whole number
you need to convert the number of your energy in a string so : function OnGUI() { GUI.Label(Rect(0, 0, 200, 64), "Energy Display" + energy.ToString()); }
and look at Rect ( posx,posy, sizex,sizey), when your size, like above, is 0 you won't see anything.
With a copy and paste of your script and applying the above code, it showed "Energy Display 100".... ignoring the missing space above.
You can also round the number if you wish?
Your answer
![](https://koobas.hobune.stream/wayback/20220612095011im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Display "organized" string on GUI Label 1 Answer
Display text with unknown length? 1 Answer
Find what word is selected by mouse in GUI.Label 0 Answers
Optimizing OnGUI 1 Answer