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 /
avatar image
2
Question by AmazingDeveloper2 · Mar 27, 2016 at 08:02 PM · unity 5inheritancemonobehaviourcoroutines

Serializable class with coroutines?

I have a class, which I want to make serializable (to see some public variables in the inspector), but I also need to use Coroutines in that class. To use Coroutines in my class I must inherit it from MonoBehaviour. But then I can't use features of the serializable class.

 public class Act1HomeAwake : MonoBehaviour
 {
     public Act1_1HomeAwake act1_1HomeAwake;
     
     public void StartAct1(int subActNumber)
     {
         switch(subActNumber)
         {
             case 1: act1_1HomeAwake.StartSubAct1_1(); break;
         }                    
     }
 }
 
 [System.Serializable]
 public class Act1_1HomeAwake
 {
     // don't see this 2 variables in the inspector WITH inheriting from MonoBehaviour
     public OpenCloseAnimation openCloseEyesScript;
     public Text textTipsTasksComponent;
 
     // WITHOUT inheriting from MonoBehaviour compiler don't understand this construction
     StartCoroutine("OpenCloseEyesAnimation");
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Mar 27, 2016 at 11:40 PM

To use coroutines you don't need to derived your class from MonoBehaviour. You just need "some" MonoBehaviour instance to run your coroutine on. So the StartCoroutine call need to be done on a MonoBehaviour, but that could be a manager class. I made the CoroutineHelper class which represents such a manager. It's just a simple MonoBehaviour singleton. You can define coroutines (generators i.e. methods that return an IEnumerator) in any kind of class. You just need a MonoBehaviour to actually run the coroutine.

In your example you could simply do:

 public class Act1HomeAwake : MonoBehaviour
 {
     public Act1_1HomeAwake act1_1HomeAwake;
     
     public void StartAct1(int subActNumber)
     {
         switch(subActNumber)
         {
             // use StartCoroutine of this monobehaviour
             case 1: StartCoroutine(act1_1HomeAwake.StartSubAct1_1()); break;
         }                    
     }
 }
 
 [System.Serializable]
 public class Act1_1HomeAwake
 {
     public IEnumerator StartSubAct1_1()
     {
         // implement your coroutine here.
         yield return null;
     }
 }

Another common solution would be to "pass" a MonoBehaviour instance to a method in a normal class. The normal class can use that reference to call StartCoroutine:

 public class SomeMonoBehaviour : MonoBehaviour
 {
     public SomeClass obj;
     void FooBar()
     {
         obj.DoSomething(this); // pass reference to this MonoBehaviour
     }
 }
 
 [System.Serializable]
 public class SomeClass
 {
     public void DoSomething(MonoBehaviour mb)
     {
         mb.StartCoroutine(SomeCoroutine()); // use the passed MonoBehaviour to start the coroutine.
     }
     IEnumerator SomeCoroutine()
     {
          yield return null;
     }
 }

  


Comment
Add comment · Show 1 · Share
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 AmazingDeveloper2 · Mar 28, 2016 at 06:17 AM 0
Share

You are genius, thanks =)

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

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add default behavior of OnDestroy function for all MonoBehavior scripts? 1 Answer

Working with Items and Class Inheritance for an RPG Item Database 1 Answer

GameObject is null when assigning to non-monobehaviour public static class 1 Answer

Coroutines white death causes? 0 Answers

What is the signature of Awake() and Start() 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