As long as there is at least one object in the scene that inherits MonoBehaviour (and there always should be), then you can find it and use it as a surrogate for managing coroutines in your class. But, the only thing that this avoids is the requirement to pass a MonoBehaviour object specifically to it, which really isn’t all that impressive.
using UnityEngine;
using System.Collections;
public class Test
{
private MonoBehaviour _mb; // The surrogate MonoBehaviour that we'll use to manage this coroutine.
public void StartCoroutine()
{
Debug.Log("Starting...");
_mb = GameObject.FindObjectOfType<MonoBehaviour>();
if (_mb != null)
{
Debug.Log("Found a MonoBehaviour.");
_mb.StartCoroutine(CoroutineTest());
}
else
Debug.Log("No MonoBehaviour object was found in the scene (which should basically be impossible).");
}
private IEnumerator CoroutineTest ()
{
Debug.Log("Hello");
yield return new WaitForSeconds(1);
Debug.Log("World");
}
}
ps
Just keep in mind that FindObjectOfType isn’t a very cheap operation. So, if you’re going to be calling coroutines this way frequently, you should cache the MonoBehaviour object that you find for future coroutine calls and avoid calling FindObjectOfTime every time you want to start it.
pps
This will probably fail if the MonoBehaviour object that it finds is disabled.
public class SomeClass
{
IEnumerator DoSomething()
{
yield return new WaitForSeconds(5);
Debug.Log("Done");
}
}
public class SomeMono : MonoBehaviour
{
public IEnumerator DoCoroutine(IEnumerator cor)
{
while (cor.MoveNext())
yield return cor.Current;
}
}
// then you could do this somewhere in SomeClass
someMonoInstance.StartCoroutine("DoCoroutine", this.DoSomething());
Im at work so cant give you the exact code atm, but i have a custom wait function that sets the current time to a variable, adds n seconds then when current time equals the modified time returns a bool as true. Its not the cleaneat in the world, but it works for my purpose.
Couldnt you derive the base class from monobehavior then access those functions through the child( again, not home cant test, never tried).
This throws the following error:
An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StartCoroutine(string, object)’