- Home /
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;
}
Answer by zz74b · May 10, 2012 at 11:19 AM
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.
Answer by Fabkins · May 10, 2012 at 11:42 AM
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];
Note the above is based on having 10 textures rather than 9, so adjust accordingly.
Lastly, if the health bar is just the typical kind of tube strip, you could consider rescalling the graphic ins$$anonymous$$d. Obviously if you have something more elaborate than your solution stands.
Your answer
Follow this Question
Related Questions
Life Player loads another level 1 Answer
Player Heal *Help* 0 Answers
I need help with the player's health points 1 Answer
Spinning Pointer that reacts when health drops 0 Answers
How to make a camera's background be another camera's veiw 1 Answer