Game Development Reference
In-Depth Information
Strings, or character strings, are strings of characters that can be letters, numbers, or other
characters. They are identified by always being enclosed in quotation marks. Beware of trailing or
leading spaces when referring to strings. the string "This is a string" is different than "This is a
string " because of the trailing space at its end. Note also that the number 4 has a value of 4 , while
the string "4" is merely the character 4 and has no value.
Booleans, named after George Boole, an English mathematician and logician, have a value that is
either true or false . The Boolean type uses the least amount of memory. It is typically used as a flag
for keeping track of the state of an object or objects.
As you get more familiar with how Unity handles communication between various gameObjects and
their components, you will discover that they can be defined as types as well.
Also worth noting is that variable and function names are case-sensitive. If your code is not working,
the first place to look is in your naming. A variable named myVar is a different variable than myvar .
Now that you've had a quick rundown of types, it's time to see how to implement some variables in
your new script. At this point, the variables won't do anything, they will just serve as a place to store
values of the appropriate type.
Besides the actual syntax, you will have to know where to put the variables. In C#, the “scope” of
the variable is determined by its location. The variables you will be adding now will be available to
anything within the class itself.
1.
In the script editor, below the class declaration, press <enter> a couple
of times, and then tab over until you are even with the rest of the class's
contents.
Indentation and extra line feeds or carriage returns are optional in C#, but they are invaluable for
keeping code neat and easy to read.
2.
Add the following:
int legs;
At its simplest, your first line of code contains the type int , or integer, the name of the variable, legs ,
and a semi-colon signifying the end of the instruction (Figure 5-3 ).
 
Search WWH ::




Custom Search