- Home /
Question by
Mohnishbala · Jun 15, 2020 at 09:17 AM ·
coroutinecoroutine errors
Coroutine problems PLEASE HELP ME
so when i run the game, the coroutine works. However after I go to the menu (in the game) then get back to the level then it just does not work. Its like the coroutine was skipped as all the things after it worked. Can someone please tell me clearly as I am a beginner. This is the code that I used
void Start()
{
countdownDisplay.gameObject.SetActive(true);
StopCoroutine(Countdowntostart());
StartCoroutine(Countdowntostart());
Debug.Log("its workin");
IEnumerator Countdowntostart()
{
while (countdownTime > 0)
{
countdownDisplay.text = countdownTime.ToString();
yield return new WaitForSeconds(1f);
Debug.Log("+1");
countdownTime--;
}
countdownDisplay.text = "Go!";
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("Main");
}
}
Comment
your code shows Countdowntostart() defined inside Start(). Are you using DontDestroyOnLoad()?
Answer by RodrigoAbreu · Jun 28, 2020 at 11:25 AM
Hey @Mohnishbala, what's up!? You should separate the coroutine from your Start:
void Start()
{
countdownDisplay.gameObject.SetActive(true);
StartCoroutine(Countdowntostart());
Debug.Log("its workin");
}
IEnumerator Countdowntostart()
{
while (countdownTime > 0)
{
countdownDisplay.text = countdownTime.ToString();
yield return new WaitForSeconds(1f);
Debug.Log("+1");
countdownTime--;
}
countdownDisplay.text = "Go!";
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene("Main");
}
Is this everything you have in this script??