[Help] Using Coroutine to add Yield return so my code will wait a frame before displaying text

So when i sat and brainstormed ideas for my Text101 i decided to stick to the prison theme.
i start by having a welcome text and a press enter to continue
then i have it go on to the prison warden talking to you the inmate and i wanted to have pauses in the text so my next page of text dosent just flash up without giving you chance to read the last page of text so i did a little research and found this link https://forum.unity3d.com/threads/how-can-i-make-a-c-method-wait-a-number-of-seconds.61011/ 5 post down a guy called hoff gives an example code that i decide to try and use i used F8 to build the code in monodevelop and it through back no errors

Here is my code: http://pastebin.com/RjZHzMpP

What i expected to Happen:
I expected that when i ran my code it would regester the input.getdown keycodes and bring up my text that then after my set pauses would move onto the next part of the code

What Happend:
so i get my first welcome text prompt but when i press entrer it dosent register my inputs

What iv done:
i tryed to do some reasearch but couldnt find anything that would help me im still at an early stage of lurning C# so i dont know what i should do to trouble shoot this problem

Maybe im just trying to step too far forward too early (if so say so) plus i dont really undertand too much what StartCoroutine is and how it all works but if this is covered later on id happily wait for that section and simplify my code and come back later when iv compleated more of the course

Thanks for any help you can give

Hi @Ashley_Mosey,

It’s a bit of an odd one this, for Text101 at least, e.g. trying to delay the output of the next action, with the action only being to display the next piece of text for the story…

I’ve just put together a simpler version of what you’ve displayed to give you an example you could test;

private void Start() {

private IEnumerator DelayedTextToConsole() {
    Debug.Log("Here is a bit of text");
    yield return new WaitForSeconds(3f);
    Debug.Log("Here is a bit more text, 3 seconds later");
    yield return new WaitForSeconds(3f);
    Debug.Log("And a bit more text, 3 more seconds later");

So, Start() gets called when the script runs, note that this only happens once. Your coroutine is then called, in which we display 3 pieces of text to the console, with a 3 second gap between.

Now, what you’ve done is to call the coroutine from the Start() method, again, remembering this itself is only called once, but in that method you are hoping to capture someone pressing a key on the keyboard, in that fraction of a second that that method will be run for. Previously your code to detect key presses was within the Update() method which was being called once per frame and thus detection was more possible/probable.

I think the first thing you should consider here is the reason why you want to delay the text from appearing, what benefit will it generate?

If you are certain that you then want to do this you will most likely need to move each piece of the story to a separate method, within an Update() method check for the current state and the key presses, then call the appropriate methods, within each of these then having another method call via a coroutine which can delay the appearance of the next piece of text.

This would take what is a fairly simple introductory game to a significantly more complex solution, but again I am drawn to the why?

I may well be missing something.

Updated Thu Nov 24 2016 16:03

I was missing something, my apologies, I missed that you said;

i wanted to have pauses in the text so my next page of text dosent just flash up without giving you chance to read the last page of text

On that note, consider that the text which tells the player which keys to press is after the main story text, the player would have already read the story text before the instructions for which keys to press.

Maybe making the text for the options to appear after a few seconds, or fade in etc, would work? Again, you’d most like want it all in separate methods, and then after calling the method to display the appropriate piece of story text, call the coroutine to make the options text appear a few seconds later. Might be best to have the options text in a separate UI.Text object also…

Here’s a link to the coroutine documentaton which will hopefully be of use too;

Privacy & Terms