CoRoutine without MonoBehavior possible?

Hello,

I just started using Unity and I would like to know if there is a way to bypass an issue I’m having with Coroutine in Unity.

I created a singleton inheriting from MonoBehavior that will be shared between scenes. This singleton (StatesManager) contains a scene state member that will be set by the game between each scene. Upon the call on StatesManager::Update() or StatesManager::UpdateGUI() methods of that singleton, the actual set state’s UpdateState() and UpdateStateGUI() methods will be called. Once the state is changed by changing of scene, UpdateState() and UpdateStateGUI() from the new state will be called instead by the StatesManager.

My problem is that inside that scene state, I am calling different objects (as an example, a GUI file) which do not inherit from MonoBehavior. How am I supposed to use Coroutine in that case?

An example is that I have a state set in StatesManager, StartMenuState, and that state, just like all others, is not inheriting from MonoBehavior as I want my StatesManager to be the one having Update()/UpdateGUI(). Upon creation of the StartMenuState, I create all objects that are required by the state, such as a StartMenuGUI object. Inside that GUI object, I have code that creates 3 rectangles and handle the case when I click on one of the rectangle, I need to launch a StartCorouting(WaitForRequest()) on a WWW object. However, since StartMenuGUI does not inherit from MonoBehavior, Coroutine won’t work… I thought about making it inheriting from MonoBehavior, but I won’t be able to call ‘new’ on the StartMenuGUI class. Any idea on how I should handle this type of situation? What could I do to be able to create my different systems inside my state that requires Coroutine stuff?

Create a dummy object (DontDestroyOnLoad if you like), attach a simple monobehaviour to it (doesn’t have to do anything except perhaps register itself as a singleton) and then start the coroutines on that. You have to watch out for potentially null things happening inside the coroutine if objects are being loaded and destroyed between scenes.