Time for action – instantiating the BeginState class
We are going to instantiate the BeginState
class to create an instance object in the memory. This will demonstrate how objects are created. Before we can do this, we first need to attach the StateManager
script to a GameObject. To do so, perform the following steps:
Attach the
StateManager
script to the Main Camera GameObject.Click on Play to show the results in the Console as shown in the following screenshot:
What just happened?
The BeginState()
method in the BeginState
class is a special method known as a constructor. It serves the same purpose as the Start()
method or the Awake()
method in a Unity script to initialize any member variables in the instance object created.
Note
A constructor method does not have a return type, not even void
.
I didn't have any variables to initialize, so I just sent a message to the Console. You could see that this method was actually called. See the first step in the previous screenshot.
The second step in...