Coroutine Reference

Hi all, quick (and I hope straightforward) question concerning coroutines. I understand to have a list of coroutines, in which each is completed sequentially, you have this:

yield return StartCoroutine ( coroutineA() );

// Will only start, when the above has completed
yield return StartCoroutine ( coroutineB() );

However, my question is, if you have a reference to the coroutine (see below) how do I achieve the same as above (i.e. start coroutineB after coroutineA has completed).

Thanks!

Coroutine m_coroutine;

m_coroutine = StartCoroutine ( coroutineA() );

1 Answer

1

You can either wrap those 2 coroutines in a seperate coroutine or instruct coroutineA to start coroutineB.

eg 1)

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

IEnumerator testCoroutineAB() {
    yield return coroutineA();
    yield return coroutineB();
}

eg 2)

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

IEnumerator coroutineA() {
    Debug.Log("A");
    yield return coroutineB();
}

IEnumerator coroutineB() {
    Debug.Log("B");
    yield return null;
}

Yup, I understand that thanks - specifically, I was after a way to control them from (in your example) the Start() function - is that possible? Cheers

Start can also be an IEnumerator. Replace the void. IEnumerator Start () { yield return coroutineA(); yield return coroutineB(); } Maybe I'm misinterpreting what you mean, not really sure what you meant by "a way to control them.". If you mean, how to have coroutines execute sequentially in a normal function, then I'm not sure if that's possible.