Time for action – assigning values while declaring the variable

Add some more variables to LearningScript using the types shown in the previous chart. While declaring the variables, assign them values as shown in the following screenshot. See how they are presented in the Inspector panel. These are all public variables so they will appear in the Inspector panel:

Time for action – assigning values while declaring the variable

What just happened?

The following screenshot shows what Unity presents in the Inspector panel:

What just happened?

The variables are displayed in the Inspector panel with the values already set.

Where you declare a variable is important

You will be declaring and using variables in many places in a script. The variables that I have shown you so far are called member variables. They are members of the LearningScript class, not declared within any method. These member variables are the only variables that have the option of being displayed in the Inspector panel or being accessed by other scripts.

So where in the class should the member variables be declared? This is another subject that can lead to heated discussions. Personally, I declare them at the top of a class file before any methods are declared so that I see them all in one place. Other people like to declare variables close to the point of first use in a method.

Note

Declaring your member variables at the beginning of a class may give you a mental clue that these member variables can be used everywhere in the script.

We will also be creating variables in methods. These variables are called as local variables and are never displayed in the Unity's Inspector panel, nor can they be accessed by other scripts. This brings us to another programming concept called variable scope.

Variable scope – determining where a variable can be used

Variable scope is a fancy way of saying "Where in the script does a variable exist". The following screenshot explains you the scope of variables:

Variable scope – determining where a variable can be used

You might have noticed that the rectangular blocks start and end with curly-braces. Just like the AddTwoNumbers() method in Chapter 2, Introducing the Building blocks for Unity Scripts, the code between the opening curly-brace and a closing curly-brace is called a code block. Absolutely anywhere in a code that you have an opening curly-brace, there will be a closing curly-brace to match. All the code between the two braces is a code block.

Notice that the code blocks can be nested inside other code blocks.

Note

You normally don't just create bare blocks of code with curly-braces like I did with Code Block 3. Code blocks are usually part of other things such as if statements, looping statements, and methods. This example is just to demonstrate how the scope of a variable works, and where a variable exists and is useable.

The following is what you have:

Line 16: string block3 = "Block 3 text";

The preceding line declares a local string variable named block3. This variable exists in the code block that is labeled Code Block 3. If you try to use the variable block3 outside of Code Block 3, such as in Code Block 2 or Code Block 1, Unity will give you an error message saying that variable block3 doesn't exist.

The scope of the variable block3 is the code block defined by the curly-braces of lines 13 and 18.

Now let's look at the block1 variable:

Line 6: string block1 = "Block 1 text";

The preceding line declares a string type member variable named block1. This variable exists in the code block that is labeled Code Block 1. This code block begins on line 5 and ends on line 20. This means the variable block1 can be used everywhere, including Code Block 2 and Code Block 3 because, they are also within Code Block 1. The block1 variable is used in Code Block 2 on line 10, and in Code Block 3 on line 14.

The scope of the block1 variable is the code block defined by the curly-braces between lines 5 and 20.

Pop quiz – knowing how to declare a variable

Q1. What is the proper way to name a variable?

Q2. How do you make a variable appear in the Unity's Inspector panel?

Q3. Can all variables in a script show in the Inspector panel?

Q4. Can a variable store any type of data?

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

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