How do i make a GUI.Label Display a float?

Im trying to make a health meter, i get an error when i put the health var into a label

Just convert it to a string like this:

var health = 10.0;

function OnGUI () {
     var healthstring = health.ToString();
     GUI.Label(Rect(10,10,100,30), healthstring);
}

sweet thanks alot

Not sure if that works as well in UnityScript (Unity’s JavaScript), but in C#, you could also do

floatVariable.ToString("#.00");

If you want it to be rendered to two digits after the dot. So that would give you, e.g. 3.23 or 123.56 instead of something like 2.5323423523 which doing ToString() might do…

Sunny regards,
Jashan

actually i ran into that problem right away,the extra digits were making my automatic layout Label bigger that its box, ill try that later

thanks

You can also use “F” instead of “#.00”

If you check the .NET docs on those formatters, you can find other useful formatting strings too.

-Jeremy

Edit: Here are some docs for you: Standard numeric format strings - .NET | Microsoft Learn