Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by zckhyt · Mar 06, 2016 at 07:53 PM · c#monobehaviourclassesienumerator

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
      }
  }

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Mar 06, 2016 at 09:10 PM 0
Share

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.)

avatar image zckhyt Owen-Reynolds · Mar 07, 2016 at 07:41 AM 0
Share

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?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges