Game Development Reference
In-Depth Information
Should you want to use a selection of these items—for example, if you do not want char-
acter sounds or animations to repeat robotically—you can also make arrays out of these
classes:
var gunSounds : AudioClip[];
var walkAnimations : Animation[];
Many new programmers worry about things like capitalization and spacing in code.
Capitalization in Unity scripting does matter, as certain commands or definitions are recog-
nized by Unity with specific capitalization. For example, Unity knows what a boolean is but
not a Boolean. Spacing is a different story: in general Unity does not care how words and lines
of code are spaced. Any indentations shown in code examples in this topic are done for orga-
nizational purposes and ease of reading, which is a good habit to develop when writing code.
Variable Scope/Visibility
Using different types of variables allows you to control how many game objects have access
to the data stored in your variables.
Public Variables The variables you have been shown so far are your garden-variety public
variable. These variables are stored within script components of objects and will show up
in an object's Inspector view when the object is selected. They can also be referenced by
other scripts and scripts attached to other objects.
Private Variables Private variables are stored within the script in which they are declared
only and do not show up in the Inspector view. Private variables are mostly used to con-
tain data that is important only to that individual script, such as collision information
or values added or subtracted from public variables in a script. They are declared with
Private var .
Static Variables Declare static variables when you want them to be accessible throughout
the entire game project. A score value that is accessed across multiple scenes of a game,
for example, should be a static variable. Other mechanics that would be declared static
would be remaining ammo, health, race results, and so forth. Static variables are declared
with Static var .
Now that you know how the data used in this chapter's scripts is stored, you can learn
how Unity uses functions to make this data run a game.
Using Functions
If variables are containers for data, functions are messages to the computer that tell it
what to do with the data provided. There are some rules and standard types of functions
that you, as a Unity developer, should know. However, you have a lot of freedom in how
you define some of your functions.
Search WWH ::




Custom Search