The question is answered, right answer was accepted
IENumerator does not work
Hi, I'm still new with c# and still learning, thats why I'm stuck around here.
What I'm trying todo is.. making the gameobject and entire script to pause and then continue after 2 seconds.
Many had put up a discussion about this, but I can't seem to get the code right.
I searched alot on the internet, and decided to use yield waitforseconds and then call using startcoroutine into my void function. But, I really don't know what I did wrong. There is no error in code, and none effect on my program too...
This is the part of the codes :
public void CheckAnswer()
{
string userAnswer;
userAnswer = inputField.text.ToUpper ();
if (userAnswer == answer) {
Debug.Log ("correct");
AudioIsPlaying.isPlayingWrongCorrect = 1;
++MyScore.score;
++MyScore.stage;
if(MyScore.stage == 2){
Application.LoadLevel ("main2");
}else{
Application.LoadLevel ("main");
}
} else {
Debug.Log ("Wrong");
AudioIsPlaying.isPlayingWrongCorrect = 2;
if(MyScore.live == 1){
MyScore.score = 0;
MyScore.stage = 1;
Application.LoadLevel ("GameOver"); //go to gameOver Scene
}else{
--MyScore.live;
txtLives.text = MyScore.live.ToString ();
StartCoroutine(WaitPopup(5)); // I can't get this work
Application.LoadLevel ("main");
}
}
}
IEnumerator WaitPopup (float duration){
Debug.Log ("test1");
popupwrong.SetActive (true); // the game object I wanted to show and then pause
Debug.Log ("test2");
yield return new WaitForSeconds (duration);
Debug.Log ("test3");
popupwrong.SetActive (false);
}
Thank you
StartCoroutine()
will return immediately after creating the coroutine... you should do the LoadLevel()
in your WaitPopup()
function after your waiting...
Thank you very much! That solved the problem. I cant believe how simple is the mistake I made. :3 Really appreciate it.