One final question: Regaining health over time

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // HERE

public class PlayerHealth : MonoBehaviour
{
[SerializeField] float hitPoints = 100f;
[SerializeField] Image healthBar = null; // HERE

public void TakeDamage(float damage)
{
    hitPoints -= damage;

    // HERE
    if (healthBar != null)
    {
        healthBar.fillAmount = hitPoints / 100f;
    }

    if (hitPoints <= 0)
    {
        GetComponent<DeathHandler>().HandleDeath();
    }

}

}

This is my player health script, which involves my health bar. This decreases as player health decreases. However, over time I want my player to regain health. How would I do this? I assume I use time.deltaTime

Like skinning a cat, there are many ways to do it. The devil is in the details.

Ok, so what would be the easiest way for achieving this?

void Start()
{
InvokeRepeating(“GainLife”, 1.0f, 1.0f);
}

void GainLife()
{
health++;
}

That would gain a single life every second.

1 Like

Did Michael’s solution fix the problem for you, @Jane_Smith?


See also:

Yes it did thank you

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms