- Home /
Odd Coroutine behavior
So I have been at this problem for the last few days and I am completely stumped. Here's the scenario:
the way I have my game setup is in Navigation phase and Battle phase, both occur in the same scene (think 2d rpg with the battle gui layered over the navigation view). My problem occurs when trying to use a coroutine to display text and then use another one to wait for user input. In the Navigation phase, it works perfectly, but for some reason, in the battle phase if you were to press spacebar (the key that proceeds to the next dialogue box) prematurely, it'd glitch the dialogue box system and have it dissappear for a moment.
Now for the sake of brevity, I will post the relevant code snippets since the source code itself is too long (Let me know if I need to put in more info though) this is how the actual coroutine utilizing the dialogue logic looks:
IEnumerator CUTSCENE_MAIN() //do not worry about this, it works fine
{
/* the two coroutines in question */
yield return DialogueMC ("(dialogue...)");
yield return WaitForUserInput();
yield return DialogueMC ("(dialogue...)");
}
and this is how I've declared both of the coroutines in question:
IEnumerator DialogueMCIE(string text){
GUIManager.PushMCDialogueBox (text, GUIManager.MainCharacterName);
yield return StartCoroutine (TextScroll(GUIInterface.instance.MC_dialogueText,text));
}
public Coroutine DialogueMC(string text){
return StartCoroutine(DialogueMCIE (text));
}
IEnumerator WaitForUserInput(KeyCode keyCode)
{
while (!Input.GetKeyUp(keyCode)) {
yield return null;
}
GUIManager.PopEverything ();//this is what clears the dialogue box to allow for preparation of a new dialogue box
}
public Coroutine WaitForUserInput(){
return StartCoroutine(WaitForUserInput(KeyCode.Space));
}
I have even tried to put yield return new waitforseconds in between the get it to wait before accepting user input to move on, but to no avail. it's like it disregards whatever command i type in and has the user skip over it anyway. my theory is that coroutines do not work as sequentially as i was hoping. like it goes ahead and starts the dialogueMC coroutine, but like a thread, and moves down the script to start the next coroutine, which is the only possible explanation as to why its accepting the spacebar input prematurely.
Again, I honestly don't understand why this is happening, but if anyone has any idea, it would help tremendously. If this is not enough info, please let me know and I will post on here the battle script in question and any relevant classes, or if a video suffices I will try and record that instead.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132925im_/https://answers.unity.com/themes/thub/images/avi.jpg)