I am trying to start a coroutine inside of another coroutine. However, I find that StartCoroutine() crashes inside of any coroutine with a NullReferenceException. In the case of the C# code below, the StartCoroutine() call in Coroutine1() is the culprit.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
StartCoroutine("Coroutine1");
}
IEnumerator Coroutine1() {
StartCoroutine("Coroutine2");
yield return null;
}
IEnumerator Coroutine2() {
Debug.Log("Coroutine2 started.");
yield return null;
}
}
Is there another approach to achieving this behavior? I certainly could write a class that would be sure to flatten out the nested coroutines – but the timing of the coroutines would be different then the ideal result from the above example.
Also, related question – Is there any way to make new YieldInstructions?