Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question sequence

Discussion in 'Scripting' started by JimWebs, Sep 27, 2023.

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    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.
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    738
    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.
     
    JimWebs likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    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:

    https://forum.unity.com/threads/des...en-restarting-the-scene.1044247/#post-6758470

    https://forum.unity.com/threads/proper-way-to-stop-coroutine.704210/#post-4707821

    Splitting up larger tasks in coroutines:

    https://forum.unity.com/threads/bes...ion-so-its-non-blocking.1108901/#post-7135529

    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

    https://forum.unity.com/threads/things-to-check-before-starting-a-coroutine.1177055/#post-7538972

    https://forum.unity.com/threads/heartbeat-courutine-fx.1146062/#post-7358312

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

    https://discussions.unity.com/t/coroutines-ienumerator-not-working-as-expected/237287/3
     
    MildredMaciel and JimWebs like this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    If you want to wait for things, use a coroutine! This holds for waiting for other coroutines too.


    Code (CSharp):
    1. public IEnumerator Someroutines()
    2. {
    3.   yield return StartCoroutine(Routine1());
    4.   yield return new WaitUntil(() => Input.anyKeyDown));
    5.   yield return StartCoroutine(Routine2());
    6.   yield return new WaitUntil(() => Input.anyKeyDown));
    7.   yield return StartCoroutine(Routine3());
    8. }
     
    Last edited: Sep 27, 2023
    Chubzdoomer and JimWebs like this.
  5. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (Input.anyKeyDown)
    4.         {
    5.             Invoke("Routine"+f,0);
    6.             f++;
    7.         }
    8.     }
    ;)
     
    JimWebs likes this.
  6. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    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.
     
  7. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    Thanks for all the replies. :)
     
    Last edited: Oct 11, 2023
  8. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    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.
     
    JimWebs likes this.
  9. MildredMaciel

    MildredMaciel

    Joined:
    Oct 9, 2023
    Posts:
    1
    Thank you sir for the links, you made my day. I appreciate you.
     
    Last edited: Oct 20, 2023