Game Development Reference
In-Depth Information
While it is obvious where the pairs are in this very small and simple script, it is easy to get layers of
nested curly brackets in more complicated scripts, so this is a good bit of script editor functionality
to remember.
As mentioned earlier, spaces and Tab-overs are optional, but by convention they are used to keep
code more readable.
Adding Contents to Functions
Functions generally contain instructions that are used to move the game forward. They may be as
simple as assigning a variable's value, or as complex as the instructions that must be carried out
if a particular condition is met. They may be equations used to calculate values for variables, or
they may be calls to other functions that can be called from several different places. Whatever the
purpose of the contents of a function, one of the most valuable means of checking your code is to
have values involved printed out to the console as the code is evaluated.
Using “Print to Console”
Earlier in the chapter, you received a few error messages from the console. Now you will put the
console to another use by having it print out messages of your own choice. The easiest way to get
feedback from your script is to have it print something out to the console. The official way to do that
is with Debug.Log() . If you put that line inside the Start function, it will get called once, when the
game starts.
1. Add the following inside the curly brackets for the Start function (Figure 5-26 ):
Debug.Log("Hello");
Figure 5-26. The new line in the Start function
Note how the string, Hello , is inside quotation marks and is color-coded a nice magenta. If several
lines go magenta colored, it's a good indication that you've forgotten a closing quotation mark.
2.
Save the script, and click Play.
3.
Open the console by clicking the Console tab or double-clicking the status
line.
The message was printed out three times, one for each of the objects that has the script on it
(Figure 5-27 ).
 
Search WWH ::




Custom Search