I've been implementing my own visual coding system for an untitled space exploration game. I had multiple places where I needed to start a coroutine from outside a Monobehaviour component.

The first class is just a class that forces a monobehaviour to persist through scenes

PersistentMonoBehaviour.cs
public class PersistentMonoBehaviour : MonoBehaviour
{
    private void Awake() => DontDestroyOnLoad(gameObject);
}

And the second class is the actual executor of the coroutine.

CoroutineExecutor.cs
public static class CoroutineExecutor
{
    private static CoroutineHandler _monoBehaviour;
    private static CoroutineHandler Handler => _monoBehaviour ??= new GameObject("CoroutineExecutor").AddComponent<CoroutineHandler>();
    public static void Execute(IEnumerator coroutine) => Handler.StartCoroutine(coroutine);

    private class CoroutineHandler : PersistentMonoBehaviour { }
}

Feel free to steal the code for your own projects :)

More posts