- Home /
The question is answered, right answer was accepted
Please help with While Loops
Ok, I have been coding for about 9 hours straight now and I think I am being monumentally dumb here.
I have this code that poisons the player, but it never un-poisons the player because the timer never reaches zero. It counts 4,3,2 and then stops.
I'm tearing my hair out here as I cant see why its not finishing the loop! Please can someone point out the obvious please!
public IEnumerator Poisoned(int timer, float delay, int amount)
{
if (isPoisoned == false)
{
int tempTimer = timer;
while (tempTimer != 0)
{
isPoisoned = true;
currentHealth -= amount;
yield return new WaitForSeconds(delay);
tempTimer -= 1;
Debug.Log("!" + tempTimer);
}
isPoisoned = false;
}
}
That code by itself is O$$anonymous$$; you have some external factor canceling the coroutine. You can test this by putting the code in a new project and running it.
Answer by NerdRageStudios · Feb 02, 2015 at 06:00 PM
Ugh, I was calling the code from a prefab that was being destroyed. For anyone having a similar issue, the coroutine dies with the monobehaviour that created it.