Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Fexception · Dec 15, 2015 at 02:14 PM · c#coroutine

Waiting for keypress from user (how to make a co-routine?) C#

Hi,

So after doing some reading and checking out everyone else's code, I'm still unsure of how a co-routine should be constructed. I noticed many people do this with a while loop. But I have a for loop that I would like to do this with, I need it to stop at the if statement and check to see what the user pressed. Suggestions?

 void LetterChecker() {
         for (int i = 0; i < gCharArray.Length; i++) {
             string charHolder = gCharArray[i].ToString();
             if (Input.GetKeyDown(charHolder)) {
                 print(charHolder);
                 
             }else {
                 print("Wrong Letter");
             }
         }
         print("Word Typing Ended");
     }
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

2 Replies

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

Answer by Jopan · Dec 15, 2015 at 03:55 PM

When using a coroutine to wait for specific input or actions, I like to do something like this.

     IEnumerator MyCoroutine()
     {
         for (int i = 0; i < gCharArray.Length; i++)
         {
             string charHolder = gCharArray[i].ToString();
 
             while ( !Input.GetKeyDown(charHolder) )
                 yield return null;
 
             print (charHolder);
         }
         print("Word Typing Ended");
     }
 

If you must check for the wrong key I would replace the while body with

     IEnumerator MyCoroutine()
     {
         for (int i = 0; i < gCharArray.Length; i++)
         {
             string charHolder = gCharArray[i].ToString();
 
             while ( !Input.GetKeyDown(charHolder) )
             {
                 if ( Input.anyKeyDown )
                 {
                     print("Wrong Letter");
                 }
 
                 yield return null;
             }
 
             print (charHolder);
         }
         print("Word Typing Ended");
     }
 
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 Fexception · Dec 15, 2015 at 05:16 PM 0
Share

This works perfectly! Thank you. I will use the first variant. Although the 2nd one will be useful during some time. I tried it and even if I press the correct letter, "Wrong Letter" still shows up. I even added an && to the if statement to exclude the fact that I pressed the correct keystroke. $$anonymous$$ept considering my correct keystroke as incorrect.

Anyway, for now this will do. I'll figure out the other way in some time.

avatar image Jopan Fexception · Dec 15, 2015 at 08:22 PM 1
Share

Glad it worked. $$anonymous$$aybe this will work for the second part.

     IEnumerator $$anonymous$$yCoroutine()
     {
         for (int i = 0; i < gCharArray.Length; i++)
         {
             string charHolder = gCharArray[i].ToString();
             
             while ( true )
             {
                 if ( Input.any$$anonymous$$eyDown )
                 {
                     if ( Input.Get$$anonymous$$eyDown(charHolder) )
                     {
                         break;
                     }
                     else
                     {
                         print("Wrong Letter");
                     }
                 }
 
                 yield return null;
             }
             
             print (charHolder);
         }
         print("Word Typing Ended");
     }
 
avatar image Fexception Jopan · Dec 16, 2015 at 08:26 AM 0
Share

When I use this, I get a "Unreachable code detected" at the top of the for loop where i++ is. I thought that yield return null should only break the while loop, not the for loop?

avatar image
1

Answer by Kimi073 · Dec 15, 2015 at 02:43 PM

This is basically one of the easiest way to make use of co-routine written in C#:

 IEnumerator YourCoroutineName(){
 // Your code that you want here 
 // In this case is the for function
 }
 void YourFunctionName(){
     StartCoroutine("YourCoroutineName");
 }

Of how to wait until the user to type then why don't you try while loop, may be it's the best and common way to use. Myself don't have any suggestion for this issue.

Comment
Add comment · Show 2 · 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 Fexception · Dec 15, 2015 at 03:47 PM 0
Share

Can I yield without a co-routine? Or is it absolutely necessary?

avatar image Kimi073 · Dec 23, 2015 at 12:36 PM 0
Share

I think you can though not sure. $$anonymous$$ost of the times I always use it in co-routine

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

40 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

MoveTowards and Lerp not working 1 Answer

StartCoroutine not listening to parameters 1 Answer

yield ends method 0 Answers

Stop previous coroutine and start new one from beginning 1 Answer

Weird problem with simple boolean and "if" statement [C#] 0 Answers


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