I feel bad posting this question since it has been posted so many times in so many different ways,
Such as here: Why does this coroutine freeze my game? - Questions & Answers - Unity Discussions
And here: Waiting for Input via Coroutine - Questions & Answers - Unity Discussions
And here also: Reddit - Dive into anything
But I have come to my wits end! Even when I copy the exact code I’m seeing on those pages, my Unity player still freezes as soon as I hit play. What I am trying to do is write a dirt-simple function that waits for a button input from the user, using a coroutine. I’ve tried several ways of doing this, and they all freeze my game:
private void SpaceBarToContinue () {
print ("Press spacebar to continue...");
StartCoroutine(WaitForButtonPress(KeyCode.Space));
}
/*
//This freezes the game
private IEnumerator WaitForButtonPress (KeyCode button) {
do {
yield return null;
} while (!Input.GetKeyDown (button));
}
//This also freezes the game
private IEnumerator WaitForButtonPress (KeyCode button) {
while (true) {
if(Input.GetKeyDown (button))
yield break;
yield return null;
}
}
//Surprise! This freezes the game
private IEnumerator WaitForButtonPress (KeyCode button) {
while (true) {
if(!Input.GetKeyDown (button))
yield return null;
yield break;
}
}
*/
//Hey maybe this time- nope! This freezes the game
private IEnumerator WaitForButtonPress (KeyCode button) {
while (true) {
if(!Input.GetKeyDown (button))
yield break;
}
yield return null;
}
When I run the debug, the program just keeps cycling forever, never giving me the opportunity to end the cycle with button input. I’ve force-quit about 20 times now trying lots of different variations on the above, and I’ve finally just decided to ask for help.
Can someone please clue me in to as what simple thing I must be missing here? I would greatly, greatly appreciate any assistance on this matter.