Game Development Reference
In-Depth Information
/* function Start () {
Debug.Log(myVar);
} */
Notice the code changes to the same gray color, indicating this function is now disabled. Save and
run in Unity and, as you might expect, the Console remains clear. Uncomment the Start() function
to enable it and save.
A variable is used for storing and retrieving data. This is how your game program remembers the
things it needs for managing gameplay.
Take a look at this line of code:
var myVar : int = 8;
Breaking it down into parts:
(1) (2) (3) (4) (5) (6) (7)
var myVar : int = 8 ;
1.
Declare a variable with the var statement.
2.
The variable name. Variable names should be descriptive, which helps to
make your code more readable. You can use any alphanumeric character
and the underscore, but no other punctuation marks or special characters.
Another good practice for making your code readable is to follow standard
programming conventions. The standard convention for naming variables is
that if the first character of the variable name is a letter, it is not capitalized.
3.
Colon separating the variable name from the data type.
4.
The data type of the variable, in this case int for integer.
5.
The assignment operator, used to assign a value to the variable you just
declared and named. You do not have to assign a value to an integer when
you declare it.
6.
The value being assigned to the variable, in this case an integer. Integers are
whole numbers, including negative numbers (indicated by a minus sign in
front). Commas and spaces are not allowed—while 24, 0, and -3 are valid
integers, 2,500 is not, but 2500 is.
7.
A semicolon indicating the end of the statement. All statements must end
with a semicolon.
In the Start() function, the Debug.Log message printed to the Console is the value of myVar . Save
and play to confirm.
 
Search WWH ::




Custom Search