How to wait between instructions, for keypress for example, inside a method

I’ve been using Unity for about two weeks (almost) and quite new to C#. I suppose I’m what you would call a newbie. So this may seem like a really stupid question:

I’d like to know how to wait between instructions, for keypress for example, inside a method. I don’t see any way to do this except in coroutines or update. But even if I use a coroutine, it will only wait for keypress simultaneously while running the method, so it still won’t wait in the method and the same with update.

Let’s say I want to show text on the screen and wait for the user to read it and click the mouse before showing the next page and so on…

public void Someroutines()
{
  Routine1();
  waithereforinput
  Routine2();
  waithereforinput
  Routine3();
}

If I use a coroutine for waithereforinput, it will not hold up the flow of the instructions in the method and neither will update.

correct, couroutines are a little like having a pal and sending them off to do something. you get to keep doing what you need. Depending on your input and what exactly you need, you that whole list to the coroutine so yield while input1 not met, yield while input2 not met etc… or, you do routine 1 i dunno set an int to 1 as you did stage 1. then when an input happens call the next routine in the list., while setting the int to 2 etc.

If this is just canned (eg, the same every time), make an animation and be done with it. You only need code in order to kick it off and even that can be automatic at scene load.

Otherwise…

Coroutines in a nutshell:

Splitting up larger tasks in coroutines:

Coroutines are NOT always an appropriate solution: know when to use them!

“Why not simply stop having so many coroutines ffs.” - orionsyndrome on Unity3D forums

Our very own Bunny83 has also provided a Coroutine Crash Course:

If you want to wait for things, use a coroutine! This holds for waiting for other coroutines too.

public IEnumerator Someroutines()
{
  yield return StartCoroutine(Routine1());
  yield return new WaitUntil(() => Input.anyKeyDown));
  yield return StartCoroutine(Routine2());
  yield return new WaitUntil(() => Input.anyKeyDown));
  yield return StartCoroutine(Routine3());
}
    void Update()
    {
        if (Input.anyKeyDown)
        {
            Invoke("Routine"+f,0);
            f++;
        }
    }

:wink:

Thanks. In the end, I did something similar, but problem I have is detecting whether mouseclick was on a button or just general mouseclick to proceed with next step.

Thanks for all the replies. :slight_smile:

How you handle this will depend on which input system you’re using. The input systems are generally event driven and so it’s easy to know if the cursor is over a particular button but not so easy to know if the cursor isn’t over any buttons at all.

It may be best to just add a ‘continue’ button. Or if all your buttons are around the edges of the screen then perhaps you can detect if the user has clicked away from the screen edges and then continue.