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 Running order

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

  1. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    How do I wait for a coroutine to finish from outside of it? It seems you can only wait inside the coroutines and then carry on with whatever you want to do from there, instead of returning to the original flow of your code after the coroutine task has finished.

    I thought about using update to check when a coroutine is finished but it makes things messy because then I'm jumping all over the place and checking flags all the time.
     
    Last edited: Sep 19, 2023
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    738
    coroutines do not block current code so

    line 1
    startcoroutine
    line 2

    line 2 does not wait for the coroutine, it forks off and does its own thing and immediatelyh line 2 follows

    There are a number of things you can do
    1. include any code you want delayed inside the coroutine so it goes

    setupstuff
    yield...
    do post stuff

    or

    2. Have a callback method
    so
    line 1
    startcoroutine(mycoroutine(callbackmethod))

    then in callbackmethod, you do the post stuff which you call at the end of the coroutine
     
    JimWebs likes this.
  3. JimWebs

    JimWebs

    Joined:
    Aug 2, 2023
    Posts:
    55
    Okay thanks a lot. The second method works for my purposes.