Time for action – creating a script and a class

We are going to create the StateManager script and the BeginState class, then add some code so that we can instantiate BeginState to make an instance of BeginState. With the help of the next screenshot, perform the following steps:

  1. In the Unity Project window, create a C# Script in the Scripts folder.
  2. Name the script StateManager.
  3. Double-click on StateManager to open it in MonoDevelop.
  4. In the MonoDevelop Solution window, right-click on the Code folder and select Add | New Folder.
  5. Name the folder States.
  6. Right-click on the States folder and select Add | New File.
  7. In the New File window, select General | Empty Class.
  8. In the Name field at the bottom, enter BeginState.
  9. Click on the New button to create the class file.

The following screenshot is the Solution window of MonoDevelop:

Time for action – creating a script and a class

With the BeginState class file open in MonoDevelop, make the changes as shown in the next screenshot:

  1. Change line 1 to using UnityEngine;.
  2. Change line 3 to namespace Assets.Code.States.
  3. Add line 9 Debug.Log("Constructing BeginState");.

Now the BeginState class file should look like the following screenshot, except for //Constructor on line 7 which I added as a visual aid:

Time for action – creating a script and a class

Open StateManager in MonoDevelop, and make the following changes:

  1. Add on line 2 using Assets.Code.States;.
  2. Press Return/Enter so that line 3 is blank.
  3. Add line 6 private BeginState activeState;.
  4. In the Start() method code block, add line 10 activeState = new BeginState();.
  5. Add line 11 Debug.Log("This object is of type: " + activeState);.
  6. Save the file.

The StateManager class file should look like the following screenshot:

Time for action – creating a script and a class

What just happened?

In the Unity script, StateManager, we are instantiating the BeginState class and storing a reference of (pointing to in memory) this new BeginState object in the variable activeState.

This basic process of creating objects will be used in the State Machine to switch from one State to another State. Instantiating classes is also what Unity is doing when it creates Components.

As an example, in the Scripting Reference, search for and select GameObject. You will see a variable listed named transform. When you click on Play, Unity instantiates the Transform class and stores a reference to the Transform Components object in the transform variable.

This means that if you need to access the data for a GameObject's Transform Components, you can simply use the transform variable instead of having to use the GetComponent<Transform>() method.

Note

As a little refresher, Transform is the name of the class file; transform is the name of the variable that will store a reference to the Transform object created when you click on Play.

This is exactly the same thing you are doing with the files you just created. The BeginState is the name of the class file. The activeState is the name of the variable that will store a reference to the to the BeginState object created when you click on Play.

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

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