public static Coroutine DelayedSetActive(this MonoBehaviour mb, GameObject target, float delay, bool value, bool stopPreviousRoutines) { if (mb.gameObject == target) { Debug.LogWarning("[MonoBehaviourExtensions] The routine is called on the same object than targeted! All routines that are ran by the object will stop, when the object is disabled!"); } CoroutineWrapper newRoutine = null; if (mb.isActiveAndEnabled) { if (stopPreviousRoutines) { ClearDelayedSetActives(mb, target); } if (!delayedSetActives.ContainsKey(target)) { // If no routines is found, add an empty list delayedSetActives.Add(target, new List <CoroutineWrapper>()); } newRoutine = new CoroutineWrapper(mb.DelayedMethod(() => { target.SetActive(value); // Remove the routine from the list delayedSetActives[target].Remove(newRoutine); }, delay), mb); // Add the routine to the list delayedSetActives[target].Add(newRoutine); } return(newRoutine.coroutine); }