- Home /
If the While Condition of a Coroutine is false is the coroutine halted?
So I have this little tidbit of code:
[Server]
IEnumerator Regeneration()
{
while (!isDead)
{
yield return new WaitForSeconds(1f);
Debug.Log("Regeneration is running.");
float regenhealth;
float regenStam;
float regenMana;
if (currentHealth < maxHealth)
{
regenhealth = healthRegenerationRate;
//Debug.Log("Player should regenerate health.");
}
else
regenhealth = 0f;
if (currentStamina < maxStamina)
{
regenStam = staminaRegenerationRate;
Debug.Log("Player should regenerate stamina.");
}
else
regenStam = 0f;
if (currentMana < maxMana)
{
regenMana = manaRegenerationRate;
Debug.Log("Player should regenerate mana.");
}
else regenMana = 0f;
if (regenhealth > 0f || regenStam > 0f || regenMana > 0f)
{
Regenerate(regenhealth, regenStam, regenMana);
}
}
}
When my player dies, isDead is set to true and the coroutine stops - as intended. My question is, is that coroutine completely stopped or do I need to kill it manually?
I'm afraid that I'm actually starting multiple coroutines all in memory with new memory allocated for each one, which will eventually kill the game.
Answer by MacDx · Mar 07, 2018 at 05:27 PM
When it goes out of the while loop the coroutine is completely stopped. You do not need to kill it manually or do anything else.
"I'm afraid that I'm actually starting multiple coroutines all in memory with new memory allocated for each one, which will eventually kill the game".
That depends on when you're calling StartCoroutine(Regeneration()); . If you're doing it inside a Start (or basically just calling it once) then there's no problem. If you are calling it inside Update then you do have a problem.
Thanks for the answer.
I thought that would be the case but I wanted to make sure.
I'm basically starting the coroutine when the player first spawns and when the player respawns - its stopped when the player dies.
Answer by Raimi · Mar 07, 2018 at 05:29 PM
Add
yield break;
at the end of the coroutine and that should make sure the corountine exits once done.
That's completely unnecessary in this case. You use yield break when you want to stop while you are at the middle of the coroutine, not at the end when it will exit on its own anyways.
Thats fine, but if he is worried about it finishing it wont hurt to add it. I just adding more info in case he didn't know about it. It wont add any noticeable overhead and clearly shows what he wants to happen in the code.
Giving him extra information is fine. Adding redundant code is not the way to go though, it's a bad habit.
For instance you'd never do this
private void Some$$anonymous$$ethod()
{
while(booleanFieldIsTrue)
{
//Foo work
}
return; //Redundant
}
Would you?
Your answer
![](https://koobas.hobune.stream/wayback/20220612154117im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is it possible to invoke a button Press With a Coroutine? 1 Answer
coroutine not running after yield new WaitForSeconds 1 Answer
coroutine help 2 Answers
problem with coroutines 2 Answers