Starting coroutine in Start method
Hey everyone,
I'm doing a couroutine for health regeneration and i start the couroutine in void Start() method but it just never starts. When i put it into void Awake() it does work.
So couroutines cannot be started from Start method?
Another question is why does visual studio sometimes put private in front of the update methods or start/awake methods?
This is my code:
void Start()
{
StartCoroutine(HealthRegeneration());
}IEnumerator HealthRegeneration()
{
while (true)
{
if (currentHealth < maxHealth.GetValue() && canRegenerate)
{
currentHealth += healthRegeneration.GetValue();
Debug.Log("Healed for " + healthRegeneration.GetValue());
yield return new WaitForSeconds(healthRegenerationTickRate.GetTickRate());
}
else
{
yield return new WaitForSeconds(healthRegenerationTickRate.GetTickRate());
}
}
}
This does not work. But when i put the couroutine start into awake it works.
void Awake()
{
currentHealth = maxHealth.GetValue();
if (canRegenerate)
{
StartCoroutine(HealthRegeneration());
}
}
Answer by OnYtoln147 · Sep 30, 2021 at 09:53 PM
Okay , the script that has this code is used in another script that derives from it so apparently that is why start method is not called. Not sure however if that is really the issue.
Your answer
Follow this Question
Related Questions
Why is a gameobject moves at start? 0 Answers
Coroutines stop randomly (Unity glitch) 0 Answers
TRIGGER TO LAUNCH GAME 2 Answers
Where have the tutorials gone? 0 Answers
How to force IEnumerator methods to be called only with StartCoroutine 2 Answers