- Home /
Problem with coroutine
Hello,
void Start () {
gameOverMenuUI.SetActive(false);
player =GameObject.FindObjectOfType<PlayerCharacterManager>();
_animator= player.GetComponent<Animator>();
}
void Update () {
if( player.getGameResult() != 0 )
{
Debug.Log("GAme over in GameOverMenu");
GameOver();
gameOverMenuUI.SetActive(true);
}
} //End update
public void Restart () {
//Resume works inversa as Pause
//In more than one scene we can select to begin again in the same scene
SceneManager.LoadScene (1);
} //End resume
public void QuitGame () {
Debug.Log("Quitting game....");
Application.Quit();
}
private IEnumerator GameOver()
{
if( player.getGameResult() == 1 ) infoDisplay.text = " Congrats !! You won" ;
else if( player.getGameResult() == 2 ) infoDisplay.text = " Enemy killed you !! " ;
else if( player.getGameResult() == 3 ) infoDisplay.text = " You run out of time" ;
_animator.SetInteger("gameResult",1);
yield return StartCoroutine(Delay(2.0F));
//player.
}
private IEnumerator Delay(float waitTime)
{
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
}
Here the important thing is that i have the gamoovermenuUI disabled, but when i noticed the game ends i want to act some way. The problem is that unity doesnt do the orders in the GameOver method. It doesnt play the animation, nor activate the gameovuermenu. I had to move the gameovermenu to the updatefunction...
What im missing here ? Where is the problem ? I wanted to do: if gameresult =1, put victory in the display info text, play an animation dancing, wait two seconds ( enough to animation being played) and then show the game over menu with the message. If i got an gameresult = 2, put defeat by killed by enemy in text, put an animation of dying, wait to let the animation be played and then show the gameover menu with the text you were killed by enemy...
Summarizing i wanted to put gameOverMenuUI.SetActive(true); after yield return StartCoroutine(Delay(2.0F)); in GameOVer function, but it doesnt work anything i wrote in it
Also would you recommend to put time.scale=0f; like menu pause just to pause all ? Seems nicer or more elegant to show the gameover menu with nothing moving at the background.... ( gameovermenu is a semitransparent background)
Thanks in advance
Answer by xxmariofer · Jan 20, 2019 at 08:18 PM
Dont call coroutines like that, use the StartCoroutine to call the coroutine method, if you put time.timescale to zero it will also stop coroutines so no, also gameOverMenuUI.SetActive(true); will be called before the end of the coroutine, since coroutines are async, if yoou want it to setactive before the coroutine put it inside of the coroutine at the end, if i am missing something else please tell me.
Answer by edufissure · Jan 20, 2019 at 08:23 PM
But as i know i was using GameOver as public void, but as inside GameOver i called the StartCorroutine it gives an error of compiler, that a void action cant have inside the yield...Thats was the only thing i put GamoIver as enumerator.
The other way i call delay with startcoroutine as you said, as ive done in other scripts... perhaps is GameOver private ?? Dont know...
Thanks
Your answer
Follow this Question
Related Questions
Coroutine doesn't work when called from another method. 3 Answers
OnCollisionEnter as IEnumerator problem (WaitForSecond) 2 Answers
Can't get past WaitForSeconds in my coroutine 1 Answer
Unity tells me that WaitForSeconds() "does not contain a constructor that takes 1 argument" 0 Answers
Delay after input? 1 Answer