How to do a function after StartCoroutine

   void Start()
    {
        Debug.Log("output1");
        StartCoroutine(saySth());
        func2();
    }


    IEnumerator saySth()
    {
        yield return new WaitForEndOfFrame();
        Debug.Log("output2");
    }

    void func2()
    {
        Debug.Log("output3");
    }

    /*
     The output will be
     output1
     output3
     output2
    */

However i want
output1
output2
output3

but i don’t want to put the { func2() } into saySth()
i don’t want to use invoke to call { func2() } also
How can i do?

thanks

Try the following:

        IEnumerator Start()
        {
            Debug.Log("output1");
            yield return StartCoroutine(saySth());
            Debug.Log("output3");
        }
    
    
        IEnumerator saySth()
        {
            yield return new WaitForEndOfFrame();
            Debug.Log("output2");
        }

It first of all turns Start into a coroutine itself. This is important because you need to delay the later bits of code within Start() until a later time, which is specifically the behavior that coroutines give you.

Then, it waits on the coroutine created for saySth(). Since coroutines inherit from YieldInstruction just like WaitForEndOfFrame and others, you can yield on a coroutine, waiting for it to finish.

What I do not know offhand is specifically when “output3” will be printed. It should definitely be after “output2”. But I don’t know if it will be immediately after and during the same frame, or if it will be delayed until the next frame. I’d hope for the former, but I’ve never tested this, and underlying implementation details have surprised me in the pass, so I’m being explicit about this ignorance. :slight_smile:

4 Likes

So personally I have a class I call RadicalCoroutine that does all sorts of weird things, including chaining coroutines. But that’s going to be a bit overkill for you.

So instead, you’ll just have to do this with another coroutine.

void Start()
{
   
    StartCoroutine(SaySequence());
}

IEnumerator SaySth()
{
    yield return new WaitForEndOfFrame();
    Debug.Log("output2");
}

IEnumerator SaySequence()
{
    Debug.Log("output1");
    yield return StartCoroutine(SaySth()); //will block until SaySth completes
    Debug.Log("output3");
}
3 Likes

is any other way to do itwithout set func2 become a IEnumerator also?

have yet another function that is an IEnumerator to call func2?

Have SaySth raise an event at the end of itself, then on that event do ‘func2’.

There’s no direct built in way… like I said, I created my own RadicalCoroutine to have events and chaining and other stuff, but it may be super overkill for you (just integrating it into your project might be a little difficult). You could build your own as well that is simplified, and reusable, but nope… no built in way.

And when I say overkill… I mean:

1 Like

Thx for guys helping. For any guys who that have the save question, try this

    IEnumerator Start()
    {
        YouClass1 youClass1 = new YouClass1();
        yield return StartCoroutine(youClass1.func1());

        YouClass2 youClass2 = new YouClass2();
        youClass2.func1();
    }

this work great.

1 Like

you use the Action parameter in your coroutine and call whatever the method you want to run after coroutine finished. Here is the 3-minute explanation.