OnEnable/OnDisable GameObjects Resume Execution

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.

So… not really. Not with the built in coroutines.

In my spacepuppy framework I have a custom coroutine type that I call RadicalCoroutine.
https://code.google.com/p/spacepuppy-unity-framework/source/browse/trunk/SpacepuppyBase/RadicalCoroutine.cs

With that you can start, stop, and resume coroutines.

Here’s an example I just slapped together that disables and re-enables the coroutine.

using UnityEngine;
using System.Collections;
using com.spacepuppy;

public class RPCDelegateTest : SPComponent {


    private RadicalCoroutine _routine;

    protected override void Awake()
    {
        base.Awake();

        _routine = new RadicalCoroutine(this.Foo());
    }

    protected override void OnStartOrEnable()
    {
        base.OnStartOrEnable();

        if (!_routine.Complete) _routine.Start(this);
    }
   
    private IEnumerable Foo()
    {
        Debug.Log("START");
        yield return new WaitForSeconds(1.0f);

        Debug.Log("Disabling!");
        //this line here will automatically stop this coroutine
        //but the RadicalCoroutine reference above will store the state of it
        this.gameObject.SetActive(false);

        com.spacepuppy.Timers.GameTimers.CreateGypsyTimer(2.0f, (t) =>
        {
            Debug.Log("Enabling!");
            this.gameObject.SetActive(true);
        });

        yield return null;
        Debug.Log("Resume...");
        yield return new WaitForSeconds(1.0f);

        Debug.Log("DONE!");
    }

}