state machines and components

is it preferable to…

  1. have states enable/disable scripts as components?
    … or …
  2. in the state script , instance them as objects and call their methods from Update() ?

Personally I have a component that acts as the state machine, and each state is a component. And I make them implement an interface that usually has a ‘StateEntered’, ‘StateExited’ and ‘UpdateState’ method (or some version there of).

And I call update via the state machine.

This way if a state needs to do things while not the active state, it still can.

Also I can let the state machine handle which update cycle (fixed, or regular) is used.

Furthermore, someone enabling/disabling the script via some other means (say the editor, or from another script), isn’t accidentally triggering what seems like a state transition to the specific state component… but really isn’t a state transition at all.

ok great thanks…

i was confused as the unity tutorials seem to promote ‘enable / disable scripts as behaviours’ but it wasn’t going well, your approach sounds better.