Time for action – adding the code to change the Scenes

We'll add a single line of code in BeginState and in LostState to change Scenes.

  1. In BeginState, add Application.LoadLevel("Scene1"); into the Switch() method on line 42 as shown in the following screenshot:
    Time for action – adding the code to change the Scenes
  2. In LostState, add Application.LoadLevel("BeginningScene"); into the StateUpdate() method on line 20 as shown in the following screenshot:
    Time for action – adding the code to change the Scenes

    If earlier in this chapter you changed WonState to switch to PlayState instead of starting over with BeginState, we're ready to test our State Machine again. If you didn't, the following are the changes you need to make to WonState:

    Time for action – adding the code to change the Scenes
  3. Save all your changes.
  4. Have BeginningScene loaded and showing in Unity.
  5. Click on Play.

What just happened?

You should now be changing Scenes as you go from BeginState to PlayState. To get back to BeginState, you have to first switch to LostState.

Pop quiz – understanding State Machine operation

Q1. The State classes implement the IStateBase interface guaranteeing certain methods are included. What is the other very important feature that the interface provides for the State Machine?

Q2. Each State has to have the StateUpdate() and ShowIt() methods. What calls these methods?

Q3. What normally happens to GameObjects when changing to another Scene?

Q4. Is this sentence true or false? Once a State Machine is setup, it can't be changed.

Verifying the code of your classes

Just in case you've had any difficulty because of constantly editing your class files, the following are the screenshots of StateManager, BeginState, PlayState, WonState, and LostState as they currently stand.

The following is the screenshot of the StateManager class:

Verifying the code of your classes

The following is the screenshot of the BeginState class:

Verifying the code of your classes

The following is the screenshot of the PlayState class:

Verifying the code of your classes

The following is the screenshot of the WonState class:

Verifying the code of your classes

The following is the screenshot of the LostState class:

Verifying the code of your classes
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.231