4
2

More than 5 years have passed since last update.

【Unity】StartCoroutine()でコルーチンを使うときの注意点

Last updated at Posted at 2017-09-10

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

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2