Time for action – accessing a variable in the current Component

Let's look at accessing a variable in LearningScript from inside LearningScript.

  1. Modify LearningScript as shown in the following figure:
    Time for action – accessing a variable in the current Component
  2. Save the file.
  3. In Unity, click on Play.

What just happened?

Here are the outputs in the Console:

What just happened?

An analysis of the code shown in the previous code screenshot is as follows:

Line 6: string myString = "Access the variable ";

  • myString is the variable that will be accessed
  • Notice that it's private by default, yet it can still be accessed

Line 17: Debug.Log(myString + "the normal way.");

  • This is how we have been accessing the value stored in a variable, by just using the variable name
  • The string value in myString, Accessing this variable, is substituted for the variable name
  • myString is being accessed without using Dot Syntax or GetComponent(), because a script always has access to its own variables and methods

Line 18: Debug.Log(this.myString + "using 'this' keyword.");

  • myString is being accessed using Dot Syntax
  • The this keyword refers to the current instance of the class, the current Component

Line 19: Debug.Log(GetComponent<LearningScript>().myString + "using GetComponent.");

  • myString is being accessed using Dot Syntax again
  • This time, the generic GetComponent<T>() method is retrieving the LearningScript Component

Line 22: Debug.Log(this);

  • Using this, the Component is sent to the Console so we can see that this is the current LearningScript Component object

Line 23: Debug.Log(GetComponent<LearningScript>());

  • Using GetComponent<LearningScript>(), the Component is sent to the Console, so we can see this also is the current LearningScript Component object
  • The this keyword and GetComponent<LearningScript>() are both retrieving the same LearningScript Component object

Whoa!! What's with line 18?

Notice item 1 in the graphic under the section, Accessing a Component's own variables and methods? This is the usual way we will access variables and methods in the current script; no Dot Syntax required. This is how we've been doing it from the beginning of this book. It's how we will probably continue to access them. However, we do have the option of accessing the variables and methods in the current Component object using Dot Syntax.

As you can see from the output of lines 17 and 18, the value stored in myString is substituted no matter how we access myString.

So if we really wanted to, we could use the GetComponent() method to retrieve the current Component object of the LearningScript class in memory, then use Dot Syntax to access myString. However, C# provides a shortcut to get the current Component object by using the this keyword.

Item 2 in the graphic is the syntax used in line 18. In this example, the keyword this simply means the current instance object of the LearningScript class, the current Component.

Note

Why do I even mention using this at this time? Later on when we get into the State Machine, we will be using this. I want you to be aware of what this is, a substitute for the current instance object of a class.

Accessing another Component on the current GameObject

Now we start to just touch on the real power of Dot Syntax, communicating with other objects to access variable data and methods. We will now communicate with another Component on the same GameObject, the Main Camera. Remember, LearningScript is attached to the Main Camera already. The following diagram will explain how this is done:

Accessing another Component on the current GameObject
..................Content has been hidden....................

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