Using StartCoroutine in a Nested Class
I'm using an IEnumerator method in a nested class, but I am having trouble calling the method from within the nested class. However, I can access it fine from outside of the class.
 public class Foo : MonoBehaviour
 {
     public class Goo : MonoBehavior
     {
         public void funcCall()
         {
             StartCoroutine(func());
         }
         public IEnumerator func()
         {
             ...
             ...
             yield return null;
         }
     }
     public void Start()
     {
             Goo goo = new Goo();
             goo.funcCall();    //    throws error
             StartCoroutine(goo.func());        // no error 
     }
 }
I put the method call that throws in the error in a try-catch statement, and printed the exception. Here is the error I am receiving:
System.NullReferenceException: at (wrapper managed-to-native) UnityEngine.MonoBehaviour:StartCoroutine_Auto (System.Collections.IEnumerator)
So it seems to be an issue I'm having with Monobehaviour to be specific, but I do not know exactly what.
I'm primarily using this to loop a block of code every frame, so if there is an alternative that I can do within the nested class, then I'm open to it.
I've seen solutions to this issue by calling the IEnumerator from outside the nested class (as I show in my template example), but I'm wanting to do it from within the nested class so I can keep the method private.
Edit: I found a solution, but it isn't ideal so I'm leaving the question open. I gave the Goo constructor a Monobehavior parameter, which I pass the current instance of Foo, as Foo inherits Monobehavior (and is therefore also one), as the argument into the constructor. I then use that instance of Monobehavior to use StartCoroutine, which works fine(?).
Here is kind've what I did:
 public class Foo : MonoBehaviour
  {
      public class Goo
      {
         Monobehavior m;
         public Goo(Monobehavior _m){m = _m;}
          public void funcCall()
          {
              m.StartCoroutine(func());
          }
          public IEnumerator func()
          {
              ...
              ...
              yield return null;
          }
      }
      public void Start()
      {
              Goo goo = new Goo(this);
              goo.funcCall();    //    now throws no error
      }
  }
The key is that you need a $$anonymous$$onoBehaviour to run a coroutine. Nested class is a red-herring here -- it's really just how to run a coroutine from a "regular" class.
I did something similar, except used a pointer to an empty gameObject which started the coroutine for me (it was shared by several classes, I think.)
The nested class did inherit $$anonymous$$onoBehaviour, but that did not work. Yet when a "regular" class inherits it, coroutines properly work (Goo inheriting $$anonymous$$onobehaviour vs. Foo inheriting $$anonymous$$onobehaviour).
And did you just create a new instance of a GameObject (GameObjecg g = new GameObject()) or create a new GameObject in-scene?
Your answer
 
 
             Follow this Question
Related Questions
Multiple PowerUps (One ItemBox) 0 Answers
"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 2 Answers
Call a method of a class derived from MonoBehaviour from a class that isn't. 1 Answer
"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers
How do I access a variable/class from a C# file in a JS file? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                