Game Development Reference
In-Depth Information
While variables hold data, functions do something. The instructions for whatever process the function
carries out is contained within the braces. Take a look at the components of the Start() function:
(1) (2) (3) (4)
function Start () {
(5)
Debug.Log(MyVar);
(6)
}
Declare the function with the reserved word function .
1.
2.
Name the function. Function names begin with capital letters.
3.
Arguments for handling information passed to the function. The parentheses
are required, so if no arguments are needed they remain empty.
4.
Open brace indicating the beginning of the function's contents.
5.
The instructions for the function to follow in the form of statements—in this
example, the Debug.Log statement.
6. Close brace indicating the end of the function. Since it is the end of a
function rather than a statement, no semicolon is used.
Notice that MonoDevelop indented the Debug statement. Indentation is used to make your code
more readable. This way, you can easily identify separate blocks of code. MonoDevelop will do this
automatically for you, but at times you will find you need to reorganize your code for legibility. In
MonoDevelop Preferences ➤ Key Bindings ➤ Edit you'll find the keyboard shortcuts +] to indent or
+[ to unindent that will shift any size block of code that you highlight. Give it a try with your Debug
statement, then with the entire Start function. Easy, right?
For a quick review of arithmetic operators (Table 2-1 ), replace your Debug line of code in the Start
function with the following lines:
print(6+2);
print(6-2);
print(6*2);
print(6/2);
print(6%2);
Table 2-1. Definitions of Arithmetic Operators
Arithmetic Operators
Meaning
+
addition
-
subtraction
*
multiplication
/
division
%
modulus
 
 
Search WWH ::




Custom Search