我有2个课程。
public class GameManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
var a = new Routine();
}
}
public class Routine : MonoBehaviour
{
public Routine()
{
StartCoroutine(test());
}
IEnumerator test(){
Debug.Log("MSG");
yield return null;
}
}
我将GameManager放在Camera上,运行游戏并获得NullReferenceException
NullReferenceException
UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) (at <d815b7efac424eeb8e053965cccb1f98>:0)
Routine..ctor () (at Assets/Routine.cs:9)
GameManager.Start () (at Assets/GameManager.cs:10)
那么这段代码怎么了?
传递给StartCorutine的枚举数不应为null,请在此处检查:https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Scripting/MonoBehaviour.bindings.cs#L88