using System.Collections; using UnityEngine; public sealed class CoroutineTicker : MonoBehaviour { private Coroutine routine; private void OnEnable() { routine = StartCoroutine(Tick()); } private void OnDisable() { if (routine != null) StopCoroutine(routine); routine = null; } private IEnumerator Tick() { for (int tick = 1; tick <= 3; tick++) { yield return new WaitForSeconds(0.25f); Debug.Log("Tick " + tick, this); } routine = null; } }