- Home /
HealthRegeneration script only works when health < 0
I found this HealthRegeneration script in another post, but i cannot seem to get it to work, it looks like this:
var CurHealth : int = 100;
var MaxHealth : int = 100;
var RegenPrSec : float = 0.10;
function Update() {
regenerate();
if (Input.GetKeyDown("i")) {
takeDamage();
}
}
function regenerate() {
if (CurHealth < MaxHealth) {
CurHealth += RegenPrSec * Time.deltaTime;
}
}
As far as I can see there is no reason for it to start regenerating when below 0 and it also only goes up to 0. I don't know much about Time.deltaTime could that be the problem?
The code here looks fine, BUT you need to take a look at the value of $$anonymous$$axHealth in the Inspector. Since the above variables are public, they will only be used when the script is attached. Any other changes to these variables will not be reflected in the game object. Any change in the Inspector will not be reflected here.
Could you specify what i need to look for, $$anonymous$$axHealth stays at 100 in the inspector
Answer by AlucardJay · Apr 03, 2013 at 03:36 PM
The problem is you are mixing ints and floats.
var CurHealth : float = 100;
var MaxHealth : float = 100;
var RegenPrSec : float = 0.10;
function Update() {
regenerate();
if (Input.GetKeyDown("i")) {
takeDamage();
}
}
function regenerate() {
if (CurHealth < MaxHealth) {
CurHealth += RegenPrSec * Time.deltaTime;
}
Debug.Log( "CurHealth as float" + CurHealth );
Debug.Log( "CurHealth as int" + parseInt( CurHealth ) );
}
Also function names should really start in a capitol, and variables should start in lowercase. Use lowercase/camelCase for variables, Capitols for functions.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095621im_/https://answers.unity.com/themes/thub/images/avi.jpg)