- Home /
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?
Answer by whydoidoit · Oct 05, 2013 at 01:42 PM
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.
For example, I have a non-monobehavior menu class, which sometimes spawns things and want to run coroutines.
For hooks to the spawnable prefabs, I already have a "menuItemHolder" script, on an empty, which is just lots of public Transform buttonPrefab;
's and no Start or Update. If anything in the scene will use the menu class, you drag in one copy of the menuItemHolder object.
So, since it's already required to be in the scene as a monobehavior, and the menu class already "knows about" it to spawn stuff, its the perfect place to add coroutines (which need to be given a reference to the menu that called them, but nothing wrong with that.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613115351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Question regarding FSM, are multiple Machines on the same Object recommended? 0 Answers
Coroutines or update method with switch statement as state machine 2 Answers
Best practice for programmatic character animation 2 Answers
Yield on a Function Typed Variable 0 Answers
StateMachineBehaviour + StartCoroutine() 2 Answers