Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by Conflei · Dec 11, 2014 at 11:39 AM · coroutinesresume

OnEnable/OnDisable Stop and Resume Coroutines

Hello everyone, i'm having a little trouble here.

In my game object i have some coroutines running and i can disable this gameobject when it's running this coroutine.

If i make gameobject.setActive(false) i will kill all the coroutines running on it and when i enable it, the gameobject will not resume them.

How can i resume this coroutines just where i stoped them? Thanks and excuse my english.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Nov 03, 2015 at 04:35 PM

Simply use a coroutine manager instead. You could have a static handle to it.

 public class CoroutineManager:MonoBehaviour{
       private static CoroutineManager instance = null;
       public static CoroutineManager Instance { 
           get {
                 if(instance == null)instance = this;
                 return instance;
           }
       }
       void Awake(){
            DontDestroyOnLoad(this.gameObject);
      }
 }

 public Other:MonoBehaviour{
       void Start(){
            CoroutineManager.Instance.StartCoroutine(MyCoroutine());
       }
       IEnumerator MyCoroutine(){ yield return null; }
 }

The downside of this is that you cannot use in the coroutine some instance variables if you plan on destroying the calling item.

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 kylebrain · Jun 22, 2018 at 10:12 PM 0
Share

Worked for me. Thanks!

avatar image
0

Answer by RakshithAnand · Nov 03, 2015 at 04:12 PM

@Conflei You cant. Coroutines will get killed when you disable the gameobject.

This maybe late but for others who would like to know,

Ref: http://answers.unity3d.com/questions/480173/how-to-not-stop-coroutines-when-deactivating-a-gam.html

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 Bunny83 · Nov 03, 2015 at 05:52 PM

It's not completely possible but with a wrapper like this it's possible:

 //ARCBehaviour.cs
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public abstract class ARCBehaviour : MonoBehaviour
 {
     private class Wrapper : IEnumerator
     {
         private IEnumerator m_Coroutine;
         private bool m_Alive = true;
         public bool IsAlive { get { return m_Alive; } }
         public object Current { get { return m_Coroutine.Current; } }
 
         public Wrapper(IEnumerator aCoroutine)
         {
             m_Coroutine = aCoroutine;
         }
         public bool MoveNext()
         {
             m_Alive = m_Coroutine.MoveNext();
             return m_Alive;
         }
         public void Reset()
         {
             throw new System.NotImplementedException();
         }
     }
 
     private List<Wrapper> m_Coroutines = new List<Wrapper>();
     public virtual void OnEnable()
     {
         ClearOld();
         for (int i = 0; i < m_Coroutines.Count; i++)
         {
             if (m_Coroutines[i].IsAlive)
                 StartCoroutine(m_Coroutines[i]);
         }
     }
 
     public virtual void OnDisable()
     {
         for (int i = 0; i < m_Coroutines.Count; i++)
         {
             StopCoroutine(m_Coroutines[i]);
         }
     }
 
     private void ClearOld()
     {
         for (int i = m_Coroutines.Count-1; i >= 0; i--)
         {
             if (!m_Coroutines[i].IsAlive)
                 m_Coroutines.RemoveAt(i);
         }
     }
 
     public void StartARCoroutine(IEnumerator aCoroutine)
     {
         var inst = new Wrapper(aCoroutine);
         m_Coroutines.Add(inst);
         StartCoroutine(inst);
         ClearOld();
     }
 }

This is a base class for your MonoBehaviour that needs "AutoResumeCoroutines". All coroutines started with StartARCoroutine will automatically be stopped when the script or the gameobject is disabled / deactivated and will resume automatically then reenabled.

However it has one little problem. When a coroutine is stopped the last yield statement is lost since that's the point where the coroutine was quit. So if you quit your coroutine at a

 yield return new WaitForSeconds(10);

when the coroutine is resumed it will resume immediately after that yield.

Also if you derive your script from "ARCBehaviour" instead of MonoBehaviour you can't simply define an OnEnable / OnDisable method since they are already defined. If you want to use those you have to override them and call the base method. Something like this:

 // ARCTest.cs
 using UnityEngine;
 using System.Collections;
 
 public class ARCTest : ARCBehaviour
 {
     void Start ()
     {
         StartARCoroutine(CoTest());
     }
 
     public override void OnDisable()
     {
         base.OnDisable();
         Debug.Log("Object disabled");
     }
     public override void OnEnable()
     {
         base.OnEnable();
         Debug.Log("Object enabled");
     }
 
     IEnumerator CoTest()
     {
         int count = 0;
         while (true)
         {
             yield return new WaitForSeconds(2);
             Debug.Log("Next " + (count++));
         }
     }
 }

Here when the gameobject is deactivated the coroutine will stop automatically. When the object is reenabled the coroutine will resume where it left off however, immediately. So if you disable and enable it quickly it won't wait 2 seconds since the last yield after the disable is lost.

It would be possible to use reflection to retrieve the time value inside the WaitForSeconds instance to somehow rebuild the last state. However some things are impossible like a yield return www; or waiting for another coroutine to finish. It would work for the "usual coroutine usecase".

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 Seed_Value · Oct 24, 2017 at 12:00 AM

http://answers.unity3d.com/questions/480173/how-to-not-stop-coroutines-when-deactivating-a-gam.html

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReferenceException in StartCoroutine 1 Answer

Game architecture problem 0 Answers

problems with coroutines 0 Answers

How to import the object from server to unity 2 Answers


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