Game Development Reference
In-Depth Information
Figure 5-5. The Legs variable exposed to the Inspector
With the variable name, note how the first letter has been capitalized for easier reading in the
Inspector. Also note that a default value of 0 has been assigned to it as well. When you declared the
variable, you did not initialize it to any value, so the default was assigned to it. Let's go ahead and
initialize the value to something other than 0 . Going back to the Animal class, 4 would probably be a
good value to start with.
9.
Change your variable line to the following:
public int legs = 4;
10.
Save the script.
11.
Click back in the Unity editor to change the focus.
This time, the Inspector doesn't update to reflect the new default value. Values set in the Inspector
always override the values that were initialized when a variable was declared. There is, however, a
way to force an update.
12.
Right-click over the Variables Test component label.
13.
Select Reset.
Now the default value for the Legs parameter reads as 4 , the value that was
initialized in the script.
14.
The only problem now is that the bench has only two legs. This is where the concept of an instance
script comes in. When you modify the script, you are modifying the master template. It doesn't
matter where you access it from—the Project view or an object in the scene—there is just one
master. The same script can be used on many different objects and will be the same until the
variables exposed to the Inspector are changed during edit mode.
1.
Drag the VariablesTest script onto the Sunflower and onto the Walkway
(the long walkway in front of the gateway).
The Sunflower could be said to have one leg, and the walkway definitely has none.
Search WWH ::




Custom Search