Time for action – modify StateManager
Edit the StateManager
class to use the IStateBase
interface. This allows the activeState
variable to store all of the State class objects. Also add the code that does the switching to the next State:
Modify
StateManager
as shown in the next screenshot.Remove the
Debug.Log
statement.Save all files.
In Unity click on Play.
Now press the Space bar key to cycle through the States.
What just happened?
The following is the output to the Console as you repeatedly press the Space bar key:
The State Machine starts with BeginState
being active. Pressing the Space bar key makes PlayState
the active State. Pressing the Space bar again makes WonState
the active State, and then pressing the Space bar key once more makes BeginState
active again.
We now have a working State Machine. For the benefits a State Machine provides, there isn't much code involved to changing States.
Let us follow the code flow for switching States:
There are no States created as of yet
The
StateManager...