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:
Add a new
static
variable on line 9.Add the
Awake()
method at lines 11 through 22 as shown in the following screenshot:
What just happened?
An analysis of the code shown in the preceding screenshot is as follows:On the StateManager
class
Line 9: private static StateManager instanceRef;
This variable named
instanceRef
stores aStateManager
type which is a reference to theStateManager
object in the memoryThis is also a
static
variableThis means, that in this example, each instance of the
StateManager
Component object that's created will share and see the same valueIt is...