Starting a coroutine not attached to object

Is there a way to start a coroutine that is not attached to any object? IE this coroutine will run regardless of whether or not any objects are disabled or destroyed? (Maybe except for switching scenes).

I know I can create a gameobject specifically for this that remains in the scene constantly, I was just wondering if there was a simple way to do it, like a static StartCoroutine() call or something.

Thanks

You can do this easily in C# using public static functions.

public static void Coroutine(){
       // do useful stuff
}

And then you can call it from any other script using className.functionName()

Some might think this is messy but I use it all the time, super useful.

Note: You can only use static variables in static functions of course.

Ok, unfortunately I found no way to start a coroutine statically, they all need to be attached to an object.

The workaround is this:

static IEnumerator MyCoroutine(GameObject obj) {
  // do something

  GameObject.Destroy(obj);
}

public static void StartMyCoroutine() {
  GameObject obj = new GameObject();
  obj.AddComponent<MonoBehaviour>().StartCoroutine(MyCoroutine(obj));
}

So I do have to create a Game Object specifically for running the coroutine, but I create it from code and don’t have to keep any redundant objects in my scene, and I automatically destroy it after the coroutine is done

//class 1 static corotine

public class Example : MonoBehaviour {

	//static class 
	public static Example ExampleStatic;
	
	
	void Awake() {
		//create static class
		ExampleStatic = this;
	}
	
	IEnumerator whatEver() {
		//code
	}
	
}

--------------------------------
//class 2

public class Example2 : MonoBehaviour {

	void Awake() {
		Example.ExampleStatic.StartCorotine("whatEver");
	}
	
}