Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Craig 1 · Feb 16, 2011 at 09:37 PM · windowrpgkeymessage

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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
 }

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Craig 1 · Feb 17, 2011 at 05:12 AM 0
Share

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.

avatar image Eric5h5 · Feb 17, 2011 at 05:30 AM 1
Share

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.

avatar image Craig 1 · Feb 17, 2011 at 07:30 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges