Coroutine not working

I’m having trouble with a damage over time script. using yield return new WaitForSeconds(1); either doesn’t wait for anything or causes the loop to only occur once.

If I put yield return new WaitForSeconds(1); inside the loop the loop will not repeat

If I put it after the loop doesn’t wait and finishes instantly.

I have tried changing the while loop to while(true) and adding an if statement in the loops to change the true variable to false but that didn’t work either.

any suggestions? i’m not getting any errors.

    public IEnumerator ApplyDamageOverTime(float time, float damage)
    {

        float perSecond = damage / time;
        float taken = 0;
        Debug.Log("Running DoT script. Taking " + damage + " damage over " + time + " seconds." + " I will take " + perSecond + " per second");
        while (taken < damage)
          {
            yield return new WaitForSeconds(1);
            taken += perSecond;
            Debug.Log("Ticking for " + perSecond + " damage. " + taken + " has now been taken");
            
        }

    }

You most likely destroyed the object where you run your coroutine on or you have disabled / deactivated the script / gameobject. Keep in mind that StartCoroutine is a member function of the MonoBehaviour class. The coroutine will run on that monobehaviour instance that you called StartCoroutine on. If that object is destroyed or deactivated you also wipe the coroutine that runs on it.