Convert Variable Float to Int for GUI.Label

I’m trying to take my player’s health and display the number over a health bar. As the player looses health, the number drops and the bar shrinks. It works well, but I need to drop the decimals after his current health so I need it as an int. But if I don’t set the current health to a float, the textured health bar doesn’t shrink properly.

I also need the texture bar set as a percent so I can adjust the player max health without having the bar itself grow bigger than the 200 pixles it’s curretly designed to fit into.

In other words, without changing the way I’ve laid this out, I just need to drop the numbers after the decimal point in line 12.

var maxHealth = 500;
var curHealth = 500f;

var healthTexture : Texture;
var emptyTexture : Texture;

function OnGUI() {
	GUI.contentColor = Color.black;
	GUI.DrawTexture(Rect(20,33,200,14), emptyTexture);
	GUI.DrawTexture(Rect(20,33,curHealth/maxHealth * 200,14), healthTexture);
	GUI.Label (Rect (25, 30, 200, 50), "Health");
	GUI.Label (Rect (100, 30, 200, 50), "" +curHealth);
	GUI.Label (Rect (122, 30, 200, 50), "/" +maxHealth);
		
	if(curHealth > maxHealth)
		{
		curHealth = maxHealth;}
	if(curHealth < 0)
		{
		curHealth = 0;}
}

You could use Mathf.RoundToInt. Unity - Scripting API: Mathf.RoundToInt

GUI.Label (Rect (100, 30, 200, 50), "" + Mathf.RoundToInt(curHealth));

Also, if I change line 2 to an int variable instead of a float:

var curHealth = 500;

I get the GUI.Label number right, but the bar doesn’t shrink. So I need line 10 to reflect the percentage of curHealth.

I tried that, wolfehunter, but then the texture bar doesn’t shrink properly.

See this topic:
http://forum.unity3d.com/threads/30618-SOLVED-typecast-float-to-int-in-javascript

GUI.Label (Rect (100, 30, 200, 50), "" +parseInt(curHealth));

Same issue, Patico. The texture bar then doesn’t shrink properly.

You need to put an f after 500 for maxHealth. Else it’ll convert maxHealth to a integer rather than a float. And add f after 200 when you multiply just so the end result will be a float.

That was it! Thank you!!