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
0
Question by gamenovice · Nov 30, 2013 at 08:36 AM · inputcoroutinesdialogue

Waiting for Input via Coroutine

Hello All,

So I've been working on a rudimentary dialogue system. Basically it works by displaying text sequentially, and to do this I am using a coroutine.

 using UnityEngine;
 using System.Collections;
 
 public class TalkTrigger : MonoBehaviour {
 
     bool currentlyTalking = false;
     public string[] dialogue;
     // Use this for initialization
     void Start () {
     
     }
 
     void OnTriggerEnter2D() {
 
         //PUT THE STARTER DIALOGUE HERE
 
         GUIManager.PushTextBox(GUIInterface.instance.genericTextBox, GUIInterface.instance.genericText, "press SPACE to talk to me");
         currentlyTalking = true;
             
 
 
         
     
     }
 
     void OnTriggerStay2D(){
         
         if(Input.GetKeyUp(KeyCode.Space) && currentlyTalking == true)
         {
             Debug.Log("Should be talking now...");
             StartCoroutine("DialogueStart");
         }
     }
 
     //this is where sequential dialogue goes
 
     IEnumerator DialogueStart(){
         GUIManager.StartConversation();
 
         foreach(string text in dialogue)
         {
 
             yield return StartCoroutine(Dialogue(text);
 
         }
         GUIManager.EndConversation();
         currentlyTalking = false;
 
     }
 
     IEnumerator Dialogue(string text){
         GUIManager.PushTextBox(GUIInterface.instance.genericTextBox, GUIInterface.instance.genericText, text);
         yield return new WaitForSeconds(5);
 
         GUIManager.PopTextBox(GUIInterface.instance.genericTextBox, GUIInterface.instance.genericText);
 
     }
 
 }


now this script works as expected, but, when i try to change:

 yield return new WaitForSeconds(5);

to

 yield return Input.GetKeyDown(KeyCode.Space);

All of the text flies by, and doesn't actually wait for the user's next press.

Is there something about the default input methods that allow this to happen? Is there a way to control the floodgate so that the coroutine pauses at each point I need user input?

Thanks in advance

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
12
Best Answer

Answer by iwaldrop · Nov 30, 2013 at 08:52 AM

If you check the scripting reference you'll see that Input.GetKeyDown returns a bool, not an IEnumerator. Since this is the case, you want to use a while loop and yield while that condition is false. Ex:

 IEnumerator WaitForKeyDown(KeyCode keyCode)
 {
     while (!Input.GetKeyDown(keyCode))
         yield return null;
 }

Now you can wait for whatever kind of input you want by calling this:

 yield return StartCoroutine(WaitForKeyDown(KeyCode.Space));
Comment
Add comment · Show 9 · 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 gamenovice · Nov 30, 2013 at 09:26 AM 0
Share

thats still causing erratic cycling behavior when i use that

avatar image whydoidoit · Nov 30, 2013 at 09:35 AM 2
Share

Looks to me like you might be starting that coroutine that does the dialogue all the time if you aren't careful. You should ensure that the Dialogue coroutine can only be running once.

avatar image gamenovice · Nov 30, 2013 at 10:10 PM 0
Share

YOU SIR DESERVE A THOUSAND $$anonymous$$EDALS!

avatar image mads232 gamenovice · Jul 22, 2016 at 01:59 AM 0
Share

Wait what am I supposed to do? I did everything up to @whydoidoit made a comment. What do you mean @whydoidoit? How am I supposed to prevent that?

It still just skips the whole inumerator/coroutine.

Tl;dr What is @whydoidoit's solution?

avatar image iwaldrop mads232 · Jul 22, 2016 at 05:50 PM 0
Share

I recommend that you create a new question, reference this issue, and post your code so that we can see what your specific issue might be. It's probably something at the call site; it's probably something simple.

Show more comments
avatar image FooTaylor · Mar 26, 2016 at 09:05 PM 1
Share

I added one line after the while, because after the input the foreach was passing through one of the array strings without stoping:

 IEnumerator WaitFor$$anonymous$$eyDown($$anonymous$$eyCode keyCode)
  {
      while (!Input.Get$$anonymous$$eyDown(keyCode))
          yield return null;
      yield return new WaitForFixedUpdate();
  }
avatar image Bunny83 FooTaylor · Mar 27, 2016 at 12:36 AM 1
Share

Yes, that's the usual way how to solve it. However there's no point in using WaitForFixedUpdate. Just do another yield return null;. Another way is to reverse the order by doing a do-while loop ins$$anonymous$$d of a normal while. That way you always wait at least once before you check the key:

 do
 {
     yield return null;
 } while (!Input.Get$$anonymous$$eyDown(keyCode));


avatar image JG0328 · Mar 23, 2018 at 02:53 AM 0
Share

Thanks!! ^_^

avatar image
2

Answer by Pablomon · Jun 14, 2018 at 12:21 AM

Inside your coroutine you could do this: yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.A));

Comment
Add comment · 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

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

24 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

Related Questions

Very fast User Input Game in Unity 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Input management with coroutines in a fighting game. 0 Answers

Why is my function/coroutine executing a second time? 0 Answers

Checking Input on 2 different coroutines at the same time 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