Time for action – adding the Awake method to StateManager

In the code block of the Awake() method, we're going to check if the GameManager GameObject already exists. If it doesn't, we'll save it to a variable and tell Unity not to destroy it when any other Scene level is loaded. If it does already exist, we'll tell Unity to destroy any new GameManager GameObjects created.

Insert the new code as shown in the following steps:

  1. Add a new static variable on line 9.
  2. Add the Awake() method at lines 11 through 22 as shown in the following screenshot:
    Time for action – adding the Awake method to StateManager

What just happened?

An analysis of the code shown in the preceding screenshot is as follows:On the StateManager ...

Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.