- Home /
Tap to play again: wait for input
If the Player gives a wrong answer, the scene should be shown until it gets a touch so the actual scene can be reloaded and played again by the player.
void Update()
{
if (right answer)
{
do something
}
else
{
StartCoroutine(waitForInput());
}
}
private IEnumerator waitForInput()
{
while (!Input.GetMouseButtonDown(0))
{
Debug.Log("wait for input");
yield return null;
}
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
The problem right here is, that if the player gives an wrong answer the actual scene is reloading without waiting for an input. So basically the while loop is not working and i don't know why. Does anybody have an idea?
Answer by swanne · Oct 12, 2021 at 04:24 PM
Hey,
Instead of waiting whilst there is no input, how about just triggering the scene when there is an input?
void Update()
{
if (right answer)
{
//do something
}
else
{
Debug.Log("waiting for input");
if(Input.GetMouseButtonDown(0))
{
Debug.Log("Got an input!");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}
This isn't working since i have already an if-statement for an Input of the mouse at the beginning of the Update function. Therefore it will instantly reload the scene after the click on the wrong answer.
hmm.. ok, how about wrapping it in an IF statement using a new bool?
if(answered && correct) { //do something } if(answered && incorrect) { if(mouse clicked) { loadscene }
}
Okey, i got it fixed now :) The problem was, that Input.GetMouseButtonDown stays true for an whole frame. I did not even know that. Looked it up in the docs. So basically i had to add an yield return new at the beginning of my while loop.
Your answer
![](https://koobas.hobune.stream/wayback/20220613044110im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Brain exploding ... Problem with an internal Coroutine manager 3 Answers
IEnumerator stops at yield 1 Answer
Custom fixed update cycle? 6 Answers
Using WaitForSeconds and trigger 1 Answer
Item duration doesn't stack 3 Answers