Hi All.
I’ve created a coroutine that displays a message, than waits for a specific key input (KeyCode.E in that case), and displays the next message.
My problem is, that sometimes when pressing E, coroutine skips past more then 1 message.
This is 100% reproducable, so I’ve worked around it by simply putting two calls two coroutine waiting for input instead of one, however, I would like to rootcause this issue and properly fix it.
Here’s part of my code:
public IEnumerator EndOfTutorialMessages()
{
DisplayMessage("Congratulation!
You’ve finished tutorial!
Press <color=red>E to continue", false);
yield return StartCoroutine(WaitForKeyDown(KeyCode.E));
yield return StartCoroutine(WaitForKeyDown(KeyCode.E));
DisplayMessage("Here's some aditional tips for you
Press <color=red>E to continue", false);
yield return StartCoroutine(WaitForKeyDown(KeyCode.E));
DisplayMessage("Press <color=red>R</color> at any time to restart level
Press <color=red>E to continue", false);
yield return StartCoroutine(WaitForKeyDown(KeyCode.E));
}
IEnumerator WaitForKeyDown(KeyCode keyCode)
{
while (!Input.GetKeyDown(keyCode))
yield return null;
}
public void DisplayMessage(string message, bool disable, float howLong = 0)
{
tutorialMessage.text = message;
tutorialMessage.gameObject.SetActive(true);
if (disable)
Invoke("DisableTutorialMessage", howLong);
}
As you can see, after the first “Congratulation!” message, I had to apply two calls to “WaitForKeyDown” coroutine, otherwise pressing E once, skips straight to the third “Press R…” message every time.
However, for second and third message this issue doesn’t exist, and pressing E once goes to the correct next message without skipping anything.
I’ll be thankful for any help with this.