Time for action – setting up another Scene

We're going to create a new Scene for BeginState. As a result, we don't want the StateManager object to be a Component of the Main Camera anymore. We'll add StateManager to another GameObject in the new Scene. In order to do so, perform the following steps:

  1. Remove the StateManager Component from the Main Camera.
  2. Make a new Scene by selecting on the menu File | New Scene.
  3. In the menu, navigate to GameObject | Create Empty.
  4. Rename the GameObject to GameManager.
  5. Add the StateManager script to the GameManager.
  6. In the menu, navigate to File | Save Scene as....
  7. Save the Scene as BeginningScene in the Scenes folder.
  8. Click on Play to verify if the State Machine is working as before.

At this point, after clicking on Play, you will see the Press to Play button and the countdown timer in your Scene.

Add the Scenes to the Build Settings as shown in the following steps:

  1. In the menu, navigate to File | Build Settings....
  2. In the Build Settings window, click on the Add Current button to add the BeginningScene to the Scenes In Build section.
  3. Drag Scene1 into the Scenes In Build section.
  4. Close the Build Settings window.
    Time for action – setting up another Scene
  5. Edit BeginState to delete line 17, timeScale = 0;, and 43, timeScale = 1;.

What just happened?

By using a separate Scene while BeginState is active, there's no longer any reason to pause the game.

In the upper right corner of Build Settings, you can see that BeginningScene is index 0 and Scene1 is index 1. If you build this project, index 0 will be loaded automatically. It doesn't work this way in the Editor. Whatever Scene is loaded runs when you click on Play.

Changing Scenes destroys the existing GameObjects

We have two issues when using more than one Scene but both can be easily remedied:

  • Every GameObject in the current Scene gets destroyed when another Scene is loaded.

    When the game starts in BeginningScene, the GameManager is created with the StateManager Component and BeginState being active. When BeginState switches to PlayState, Scene1 will be loaded and our GameManager will be destroyed.

  • Every time BeginningScene is reloaded, another GameManager with another StateManager Component is created.

    That simply isn't going to work very well. Throughout the game there needs to be only one GameManager with one StateManager.

We want the first GameManager with its StateManager Component to always be available. After all, it's the controller for the whole project. To prevent GameManager from being destroyed when switching from BeginningScene to Scene1, Unity provides a method called DontDestroyOnLoad().

We will add this in a moment, but the second issue has to be dealt with at the same time.

In our State Machine, we have LostState switching back to BeginState. The issue is that when BeginningScene is reloaded, another GameManager GameObject will be created. We don't want another one. We just want the first one we created when we first clicked on Play.

This is easily remedied as well by detecting that our first GameManager already exist and immediately destroying any new ones that are created.

Keeping GameManager between scenes

Both of these issues will be fixed in Unity's Awake() method. This method is called on any Component that invokes Awake(), right after a Scene is loaded.

..................Content has been hidden....................

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