How to do a fractional percentage

I’m looking to do a health bar and looking at the answer here: to draw a health bar - Questions & Answers - Unity Discussions

However, ‘playerHeathBar = playerHealth / playerHealthMAX;’ seems to just result in zero, both when I print it out and in a tiny bar on the screen that does not change when playerHealth is adjusted. It seems the fraction is being removed? I’ve been searching around for about fifteen minutes and trying variations to find some way to avoid that occuring and cannot find any. What’s the method?

float playerHeathBar = (float)playerHealth / (float)playerHealthMAX

force to cast values to float. integers doesn’t have fractional part

Here is the edited code again, although I have fixed the original answer (on the other question). Never realized the errors as there was no reply to that question, with 450+ views am surprised no-one else mentioned it !

var healthTexture : Texture2D; // this is the texture displayed in the GUI.Box
var playerHealth : float = 75.0; // use your health var here 
var playerHealthMAX : float = 100.0; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage

function OnGUI() 
{
	playerHealthBar = playerHealth / playerHealthMAX;   // playerHealth as a percentage
	if (playerHealth > 0)
	{
		// GUI.Box(Rect((Screen.width/2) - 100, 10, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
        GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 110, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true); // much nicer looking display
	}
}

Edit : Here is a direct copy-paste of the code I tested and is working for me. My texture is 512x52 pixels

var healthTexture : Texture2D; // this is the texture displayed in the GUI.Box
var playerHealth : float = 75.0; // use your health var here 
var playerHealthMAX : float = 100.0; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage

function OnGUI() 
{
    playerHealthBar = playerHealth / playerHealthMAX;   // playerHealth as a percentage
    if (playerHealth > 0)
    {
       GUI.Box(Rect((Screen.width/2) - 100, 40, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
       GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 10, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
    }
}

2982-gui_health.png

here is the script for the above picture :

#pragma strict

function Start () {

}

function Update () {

}

var healthTexture : Texture2D; // this is the texture displayed in the GUI.Box
var playerHealth : float = 75.0; // use your health var here 
var playerHealthMAX : float = 100.0; // use your MAXIMUM health var here (to calculate a percentage from)
private var playerHealthBar : float; // just a var that holds playerHealth as a percentage

function OnGUI() 
{
    playerHealthBar = playerHealth / playerHealthMAX;   // playerHealth as a percentage
    if (playerHealth > 0)
    {
       GUI.Box(Rect((Screen.width/2) - 100, 10, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
       GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 40, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
		
		playerHealthBar = 0.05;
       GUI.Box(Rect((Screen.width/2) - 100, 80, (200 * playerHealthBar), 20), healthTexture); // health bar is 200 wide at maximum health
       GUI.DrawTextureWithTexCoords( Rect((Screen.width/2) - 100, 110, (200 * playerHealthBar), 20), healthTexture, Rect(0, 0, (1.0 * playerHealthBar), 1.0), true);
    }
}