Using Update() or Coroutine for this problem?

Let me explain,
I have a GameStates manager which acts as an FSM to control each state of the game. When the FSM starts, it first runs the “Initialize” state in which I call a function that does some initialization and sends messages to other Managers to tell them it’s time to cache some references to other things.

If the initialization was successful, GameStates then sets a static boolean to true: intiDone = true, and switches to the next state, Playing.

Now my other classes rely on initDone to set themselves up. The problem is that sometimes, initDone only becomes true after Update() in those classes, and Update() uses variables that get set when initDone becomes true.

So 1 solution is to use: if( initDone == false ) return; - in Update(), but it seems like a waste of time because it’s always gonna have to check it before continuing.

The other solution i thought about is instead of using Update(), I would use a coroutine, that way when initDone = true, I can send a Message to all classes that are waiting for it, and start the coroutine in each class. The coroutine would run every frame just like Update().

But, I couldn’t do that for FixedUpdate(), since physics should be in FixedUpdate I cannot have a coroutine running my physics calculations.

I am running into this problem a lot because of the OnEnable() bug which force the class it belongs to to execute before any other class which doesn’t have OnEnable() defined. The whole idea behind my GameStates class was to control these kind of things, and it worked great until I started using LoadLevelAdditive() to load things like HUD, Controls and Player into each scene, instead of having to manually place them into each new scene I create.

Since those classes are being loaded after the current’s scene classes, it broke my GameStates logic and I am now trying to make it work with classes that come in after the initialization.

Sorry for the long post, and thanks in advance for your time! If anyone could maybe give me some advice I would really appreciate it :slight_smile:

Thanks guys,
Stephane

So you can do Physics in a coroutine if you use

  yield return new WaitForFixedUpdate();

You could also use a delegate in your Update function and switch it to point to something else when you are ready - though that’s probably slower than an if check - neither are going to cost you much. The delegate method is good for state machines though - because you can switch between multiple - see my FSM article on Unity Gems

  Action updateFunction;

  void Start()
  {
      updateFunction = BeforeInitUpdate;
  }

  void BeforeInitUpdate()
  {
      //Any before init update logic
      if(initDone)
          updateFunction = AfterInitUpdate;
  }

  void AfterInitUpdate()
  {
      //Normal update functions
  }

  void Update()
  {
       updateFunction();
  }

Sorry, I didn’t read this at full because is too long, I suggest you to avoid the use of the bool initDone, because Unity calls Awake and Start voids at start, after these two it will call Update, which will called every frames.
So Update will always be later then Start or Awake.
I don’t know if this was what you was asking for.