- Home /
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.
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.
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
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".
Your answer
Follow this Question
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