- Home /
How do I have Unity Wait for a keypress?
What I'm trying to do is create an RPG, and when you go and talk to people a message window comes up. I'm able to put the message window up along with the text, but I'm having trouble keeping program execution from continuing until after the user has pressed Enter. I tried making the program go into a while loop that waits until a key is pressed but that doesn't work as it causes Unity to crash right when I start the game. Is there any other alternative?
Answer by Eric5h5 · Feb 16, 2011 at 09:47 PM
Don't use Update, which executes every frame and can't be interrupted. Use coroutines.
function Start () { while (true) { while (!Input.GetKeyDown(KeyCode.Return)) yield;
// do stuff
}
}
Unfortunately I've tried this already. While loops based upon the condition of a keypress freezes and crashes the Engine. I just don't think there's any way this can be done yet.
Of course it can be done, it's very simple. It doesn't freeze or crash at all if your logic isn't broken. The code I posted, for example, as long as the "do stuff" part contains a yield somewhere, will never freeze. Replace "`//do stuff`" with "`print ("Hit return"); yield;`" and see for yourself.
Thank you so much, sir! It works like a pro now! I probably need to learn more about co-routines. Craig 1 hour ago
Your answer
Follow this Question
Related Questions
Showing and hiding guiText after a key is pressed -1 Answers
C# Capture Keypress 3 Answers
Some questions about Gui.Window 1 Answer
How to call value by key when key is an object? 1 Answer
Unity 3.0 Beta Key Question 1 Answer