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:

  1. Attach the StateManager script to the Main Camera GameObject.
  2. Click on Play to show the results in the Console as shown in the following screenshot:
    Time for action – instantiating the BeginState class

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 the previous screenshot shows that the value stored in the activeState variable is a reference to an instance object of the BeginState class.

The following is the code analysis on various classes:

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

Line 6: private BeginState activeState;

  • The activeState is a variable that can store a reference to an object of type BeginState
  • The activeState variable is private because you don't want any external code to change the stored value

Line 10: activeState = new BeginState();

  • The new operator is how you create an instance of a class
  • This means an instance of BeginState has been created in memory
  • Code flow now jumps over to line 7 in the BeginState class

An analysis of the code shown in the preceding code screenshot is as follows:On BeginState class

Line 7: public BeginState()

  • This is a constructor to initialize any member variables in the newly created instance of the BeginState class
  • A constructor method name is the same as the class name

Line 9: Debug.Log("Constructing BeginState");

  • This line is just sending a text message to the Unity Console to show that the constructor was called when the instance object was created
  • This method is now finished and code flow returns back to StateManager line 10

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

Line 10: activeState = new BeginState();

  • A reference to the BeginState instance object is assigned to the variable activeState

Line 11: Debug.Log("This object is of type: " + activeState);

  • This message is sent to the Unity Console to show you that activeState does reference an instance object of the BeginState class

Specifying a file's location with a namespace declaration

Earlier you changed line 3 in BeginState. What is this namespace on line 3?

If you look up namespace on the Internet, you might be overwhelmed with explanations. For writing classes in Unity, I'll boil it down to this:

Note

A namespace declares where a class file is located in the Unity Project folder structure.

For coding in the Unity environment, that's really all you need to know.

The BeginState file is in the States folder, which is a subfolder of the Code folder, which in turn is a subfolder of the Assets folder. It's a lot easier to use the Dot Syntax to express this location like this: Assets.Code.States. Therefore, line 3 declares this, and the whole BeginState class definition is in the namespace code block.

Now that this location has been declared in BeginState, any other class that wants to use BeginState has to specify this same location, otherwise it won't be found.

Note

If you move the file to a different folder, you will have to change the namespace as well.

Locating code files with a using statement

Look at StateManager. You added using Assets.Code.States on line 2. This means you can use any class file that resides at that location. Right now you only have BeginState there, but you will be adding more States later.

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

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