Unity-NullReferenceException(IEnumerator)=> StartCoroutine问题

时间:2018-10-08 18:29:30

标签: unity3d nullreferenceexception coroutine ienumerator

很抱歉这个愚蠢的问题,如您所知,我是新手。我可以  没办法弄清楚,半天试图取得成功的结果..  请告诉我错误在哪里?遭受这种痛苦的原因是  避免在许多文件中重复代码。

文件-1(位于场景中的空GameObject上)

 public class BattleSystem : MonoBehaviour
 {

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

   private IEnumerator StartMeleAction() 
   {
     ID1 _ID1 = new ID1(); 
     yield return StartCoroutine(_ID1.StartAttack()));
   }

 }

文件-2(继承=> BattleSystem)

 public class ID1 : BattleSystem
 {

   public IEnumerator StartAttack() 
   {
     Debug.Log("first OK!");
     yield return StartCoroutine(Test());
   }

   public IEnumerator Test ()
   {
     Debug.Log("Last OK!");
     yield return null;
   }

 }

不是期望的结果:

 first OK!
 last OK!

我明白了

    first OK!
     NullReferenceException
     UnityEngine.MonoBehaviour.StartCoroutine (IEnumerator routine) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/MonoBehaviourBindings.gen.cs:62)
     ID1+<StartAttack>c__Iterator0.MoveNext () (at Assets/Script/Heroes/ID1.cs:58)
     UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at
   C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
     UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
     <Attack>c__Iterator6:MoveNext() (at Assets/Script/BattleSystem/BattleSystem.cs:406)
     UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

0 个答案:

没有答案