How To Use WaitForSeconds in This Code

hi i want to make player gets healt if its not hitted. I did that but there is one problem right after hit its health being full because of the health++ in update. How can i make this health++ slower? I tryed waitforseconds but it didn’t work.

        if (Input.GetKey(KeyCode.E))
        {
            IsTakingDamage = true;
            TakeHit(1f);

        }else
        {
            IsTakingDamage = false;
        }

        if (IsTakingDamage == false)
        {
            while (currentHealth <= 100f)
            {
                currentHealth+= 0.001f*Time.deltaTime;
            }
        }
    }
    public void TakeHit(float damage)
    {
        currentHealth -= damage;
        Healthbar.SetHealth(currentHealth, MaxHealth);

        if (currentHealth <= 0)
        {
            Destroy(gameObject);
        }

    }

If you would just put this in the update function then it would work.

Update()
{
 if (IsTakingDamage == false)
            {
                    currentHealth+= 0.001f*Time.deltaTime;
            }
}

If you want something to happen over time then you can use the update() or coroutines.
That is the unity build in way at least.

1 Like

Agreed… get rid of that while loop on line 13.

Let me point out also that this will gain 1 health point every 1000 seconds… veeeeeeeery slowly.

You probably want something more like:

currentHealth += 3.0f * Time.deltaTime;

That will regain health 3 units per second, smoothly.

1 Like

Oh sorry i forget the paste update part. It was already on update() .

When i try like this

       if (IsTakingDamage == false)
        {
            while (currentHealth < 100)
            {
                currentHealth += 3.0f * Time.deltaTime;

            }
        }

health is getting full right after damage

now i tryed like this it is working but i cant limit to max hp (100)

        if (IsTakingDamage == false)
        {
                currentHealth += 3.0f * Time.deltaTime;
        }

okey im idiot guys sory to bother you just did this code below and its worked very well thanks to you guys.
I realized after a long time that I could do this

       if (IsTakingDamage == false)
        {
            if (currentHealth <= MaxHealth)
            {
                currentHealth += 3.0f * Time.deltaTime;

            }
        }

Just in case you ever want to use a coroutine for something similar in the future.

Increase player health by 1 every second.

private WaitForSeconds _waitTime = new WaitForSeconds(1.0f);
private bool _healPlayer;

private void Start()
{
    _healPlayer = true;
}

private void Update()
{
    if (_healPlayer)
    {
        StartCoroutine(HealPlayer());
    }
}

private IEnumerator HealPlayer()
{
    _healPlayer = false;

    playerHealth ++;
    yield return _waitTime;

    _healPlayer = true;

}
1 Like

thank you i will keep it in mind sir <3

Just for future reference, when your code is running Unity is completely locked-up solid. Nothing else happens onscreen until you return from your code. And I mean NOTHING.

The only way to do a while loop AND have other stuff happen is if that while loop is: 1) in a coroutine, and 2) has a yield return statement inside it.

Otherwise 100% of that while loop will complete (if it is capable of completing) BEFORE the next frame renders. If the loop cannot complete, your program will lock up permanently and you will have to force-close the editor, losing any unsaved changes.

1 Like

ohhh my program was keep crashing so its because of this thank you sir