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
1
Question by solpic · Aug 28, 2011 at 10:42 PM · c#coroutinemonobehaviour

Coroutine without MonoBehaviour

Hello,

Is there a way to have a coroutine without deriving from MonoBehaviour and having an instantiated GameObject. For various reasons it is necessary that this class not be a game object and thus it can't derive from monobehaviour, however I need it to be able to run coroutines. Does anyone know how to do this?

Thanks in advance

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

6 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by jistyles · May 27, 2014 at 07:49 PM

Just to share my solution in full, I use an absolute minimal number of monobehaviours, and instead I use one main one and then managed custom subcontrollers that get called through to. So in the non-monobehaviour class, define your IEnumerator like normal:

 private IEnumerator DelayedMapTransition(MapPos mapTarget, MapController.ExitTypeEnum exitType, float preDelay)
     {
         //Debug.Log("Start delay " + preDelay.ToString());
         
         yield return new WaitForSeconds(preDelay);
         
         //Debug.Log("End delay " + preDelay.ToString());
         
         MapTransition(mapTarget, exitType);
     }

And then in your master monobehaviour, create an interface to call through to:

 //To support external coroutines being called:
 public void StartChildCoroutine(IEnumerator coroutineMethod)
 {
     StartCoroutine(coroutineMethod);
 }

And then finally the non-monobehavoiurs' can coroutine to their little hearts content, eg:

 public void MapTransitionDelayed(MapPos mapTarget, MapController.ExitTypeEnum exitType, float preDelay)
 {
     SceneController.instance.StartChildCoroutine(DelayedMapTransition(mapTarget, exitType, preDelay));
 }
Comment
Add comment · 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
7

Answer by IgorAherne · Oct 20, 2016 at 04:21 PM

Good solution above @jistyles, except you don't need StartChildCoroutine( )

just use

 SceneController.instance.StartCoroutine(YourCoroutine(args)){ }

Because its a MonoBehavior anyway. You don't need this interface

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 Naphier · Oct 21, 2016 at 03:07 AM 0
Share

Yup, StartCoroutine is public, no interface needed!

avatar image
-1

Answer by SolidAlloy · May 31, 2020 at 01:16 PM

There is a free asset that allows you to do that without writing your own implementation: More Effective Coroutines

Besides, it allows you to run coroutines in disabled MonoBehaviours, and its coroutine implementation allocates less memory.

Comment
Add comment · 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
0

Answer by RealSoftGames · Apr 13, 2017 at 11:58 AM

you can do it a few different ways

one is using a singleton on a monobehaviour object another is to create an instance of the class and pass it in that way.

 public class Test
 {
     private void RunCoroutine()
     {
         Test2.t.GetComponent<Test2>()._StartCoroutine(enumerator());
     }
 
     public IEnumerator enumerator()
     {
         yield return new WaitForSeconds(1);
     }
 }
 
 public class Test2 : MonoBehaviour
 {
     public static GameObject t;
 
     private void Awake()
     {
         if (t != gameObject || !t)
             t = gameObject;
     }
 
     public void _StartCoroutine(IEnumerator iEnumerator)
     {            
         StartCoroutine(iEnumerator);
     }
     public void _StartCoroutine()
     {
         Test t = new Test();
         StartCoroutine(t.enumerator());
     }
 }
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 Bunny83 · Apr 13, 2017 at 12:24 PM 0
Share

Why would you use a static "GameObject" variable and not a static "Test2" variable? Each time you want to use your singleton you would need to use GetComponent.

Also what's the point of

 public void _StartCoroutine(IEnumerator iEnumerator)

? You can simply call StartCoroutine on any $$anonymous$$onoBehaviour instance. It's a public method.

Furthermore the || !t is redundant. That condition is only checked when t is equal to gameObject. When t equals gameObject there's no point in setting t to gameObject.

A preceding underscore on a member name, by convention, indicates a private member.

avatar image
0

Answer by ElSamy · Sep 16, 2020 at 07:58 PM

Hi. Thank you @jistyles for your answer. I am conceiving a game the same way, with one main MonoBehaviour script, and other non MonoBehaviour scripts. I just don't understand how you instantiate your main script. Thank you very much for your answer. ,Hi, I conceived my game the same way, with one main MonoBehaviour and other non MonoBehaviour Class. So I would like to use your solution. But I don' understand how you instantiate your main MonoBehaviour Script. Thank you very much for your answer

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 hitarthdoc1994 · Sep 30, 2020 at 10:13 AM 0
Share

Your main $$anonymous$$onobehavior would be attached to a GameObject in scene which will handle your other scripts. I would recommend you look into Scriptable Objects for modular behaviors.

  • 1
  • 2
  • ›

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Lerp in Coroutine (Crazy Behavior) 2 Answers

Threads for Instantiate 1 Answer

Not properly understanding how monobehaviour scripts act without a GameObject. 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