increase INT over time

I see several examples of increasing a float over time using Time.deltaTime but can’t seem to get it to work with an “int”. Currently I have the players health get replenished by picking up a health power up perk. But I would like to ditch that and just have the player gradually heal himself. The player health script has currentHealth as an int so all scripts that inflict damage do so in “ints”. One option is to change several scripts making the health value a float but I would rather just get the int to increase over time. How, I ask?

Can’t you just use a float as a temporary value?

public int currentHelath;
public float healthPerSecond=2f;

private float regenHealth;
void Update(){
  if(regenHealth >= 1){
    int floor = Mathf.FloorToInt(regenHealth);
    currentHealth += floor; // normally 1 unless huge framedrop or Huge regenHealth value
    regenHealth -= floor;

I will try to explan the code:

If your currentHealth was a float then you could do just this:

void Update(){

In this case currentHealth would increment by healthPerSecond each second (I put a 2, but as it is a public variable you can change it from the inspector and increase it if you want more healing or decrease it if you want less healing), but this can’t be done as your currentHealth is an int.

So I just created another variable called regenHealth to store the amount of healing. So regenHealth will increase each frame a little for example (0.12,0.34,0.40…A second pases from the start… 2).

The next thing I do is to check if regenHealth is greater than 1, so if for example regenHealth is 1.23 I use Math.FloorToInt that returns the integer value from a float, in this example it would be 1, then I add that value to your currentHealth, and then substract it from regenHealth so it will be 0.23.

If you want to stop the healing you can set healthPerSecond = 0; If you want to make a poison effect then you can set healthPerSecond to a negative value and the life would decrease little by little.

Some things to improve the script is to have a maxHealth integer value, for example 100, and then change this line:

currentHealth += floor;


currentHealth = Mathf.Clamp(currentHealth+floor,0,maxHealth);

this way your health can’t go above your maxHealth value nor below 0.

HOLY COW. It works WONDERFULLY! thank you so much. Can’t say that I understand a single line of it ( yet) but it is so much better than health powerups. To make my game interesting to me ( the coder) I added a lot of unpredictability in enemy spawns so placing health power ups in strategic places ( when i might need them) is impossible to predict, and I didn’t like the look of dead enemies leaving health drops either ( kind of distracted from the cool ragdoll death effect)
thanks again for the prompt and successful answer