StartCoroutine()はMonoBehaviourを継承したメソッドで、
MonoBehaviour.StartCoroutine()
である。
このMonoBehaviourはprint, Start(), Update(), Awake(), OnGui()
などに絡んでくる。StartCoroutine()では重要になる。
例えば別のクラスのコンポーネントを取得するとき、
public class ClassName1 : MonoBehaviour {
ClassName2 class2;
void Start()
{
class2 = new ClassName2();
}
}
などと書いてしまうと、class2内のメソッドで
StartCoroutine()を実行したときにNullReferenceExceptionが発生する。
下記のサイトのように、ほかのクラスをほかのゲームオブジェクトにアタッチして、
class2 = GameObject.Find("class2Object").GetComponent< ClassName2 >();
とすると正常にコルーチンを実行できた。
http://unitygeek.hatenablog.com/entry/2017/05/23/111254