Regain health on GUI

Hi all i have a pretty simple script linked up to my player and when the enemy hits my player he loses health and it is visible through the GUI. However the tiny if statement at the bottom is supposed to regenerate his health over time only it doesn’t or at the very least isn’t being shown by the GUI to do so. Any help would be much appreciated.

var h00: Texture2D;
var h10: Texture2D;
var h20: Texture2D;
var h30: Texture2D;
var h40: Texture2D;
var h50: Texture2D;
var h60: Texture2D;
var h70: Texture2D;
var h80: Texture2D;
var regen : float = 5;

static var HEALTH = 80;

function Update () 
	{

	var g_Health = gameObject.Find("g_health");

	if(HEALTH == 70){
	
		g_Health.guiTexture.texture = h80;
		return;
	}
	
	else if(HEALTH == 60){
	
	g_Health.guiTexture.texture = h70;
		return;
	}
	
	else if(HEALTH == 50){
	
	g_Health.guiTexture.texture = h60;
		return;
	}
	
	else if(HEALTH == 40){
	
	g_Health.guiTexture.texture = h50;
		return;
	}
	
	else if(HEALTH == 30){
	
	g_Health.guiTexture.texture = h40;
		return;
	}
	
	else if(HEALTH == 20){
	
	g_Health.guiTexture.texture = h30;
		return;
	}
	
	else if(HEALTH == 10){
	
	g_Health.guiTexture.texture = h20;
		return;
	}
	
	else if(HEALTH == 5){
	
	g_Health.guiTexture.texture = h10;
		return;
	}
	
	else if(HEALTH == 0){
	
	g_Health.guiTexture.texture = h00;
		return;
	}

HEALTH += regen * Time.deltaTime;

}

Just a few pointers for you:

Once you have set your texture, you return from the function. Your regen code will never be run if the health == any of your statements…

your else if statements will only change the texture if the health is EXACTLY the value specified.

Health looks like it will be an integer (assuming the code above is c#) in which case the regen * Time.deltaTime will be a very small number (i.e. 0.000025). The following maths will then occur:

cast regen health to integer => closest integer to 0.000025 == 0
add 0 to Health.

That’s probably why your health never regenerates.

To fix:

Remove your return statements from within your if statements.

Change the var Health = 80; to be:

float Health = 80f;

but then you must be aware that is it unlikely that your else if(health == x) values will EVER be true. And you’ll need to change them to else if (Health < 40) etc.

Edit: Looks like you are using JS - You’ll have to convert the syntax above.

As the poster said the health value is going to default to an integer and you adding a float. Changing it to a float will fix the health regen but will mess up your if statement. So you could do the following:

static var HEALTH:float  = 80;

function Update () 
{
var tempHealth:int = HEALTH;

if(tempHealth == 70){
   g_Health.guiTexture.texture = h80;
   return;
}
/*    the other if statements using tempHealth */

HEALTH += regen * Time.deltaTime;
}

Also for effeciency , I would move the getObject call out of the update code to the statup:

function Startup()
{
g_Health = gameObject.Find("g_health");
}

Obviously make g_Health a global.

Lastly you could put all the bitmaps into an array and reference the array. Something like:

var healthTexture: Texture2D[];

Then instead of having any “if” statements just do:

var tempHealth:int = HEALTH;
g_Health.guiTexture.texture = heatlTexture[tempHealth/10];