Game Development Reference
In-Depth Information
In the example, the variable newColor is local to that function. It is created when the function is called
and a Color value is passed into it. When the function has been evaluated, the variable is destroyed,
freeing up memory. Your earlier variables' scope makes them available to any functions within the
class at any time because they were declared outside of the functions and will exist as long as the
instance of the script exists.
The final part of the function syntax is the pair of curly brackets or braces. Like parentheses, they
must always exist in pairs. The opening one follows the argument parentheses. It can be on the
same line, or even the following line. The formatting style makes no difference to the code but can
be an important convention in some companies. You will see it both ways in Unity scripts, so it is
worth being aware of (Figure 5-23 ). The closing bracket aligns with the function on the left. The code
between the curly brackets is carried out or evaluated (and acted upon) when and only when the
function is called.
Figure 5-23. Another valid layout style for curly brackets
Because many syntax errors are caused by orphaned parentheses and curly brackets, the
MonoDevelop script editor will automatically show the mate to the selected character.
1. In the script editor, click before or after the opening curly bracket for the
Start function. (Do not highlight it.)
The mate to the selected curly bracket is highlighted (Figure 5-24 ).
Figure 5-24. The mate to the opening curly bracket selected when the focus is next to the opening one
2.
Repeat the process for one of the parentheses (Figure 5-25 ).
Figure 5-25. The opening parenthesis selected when focus is at the closing one
 
Search WWH ::




Custom Search