Game Development Reference
In-Depth Information
checks to see if the statement inside the parentheses evaluates as true . If the statement does
evaluate to true , it carries out whatever instructions you set for it. There is also an optional else
clause that can carry out a different set of instructions should it evaluate as false. The syntax roughly
is as follows:
if (<some expression>) <do something>
else <do something else>
The expression that is evaluated is generally a comparison of some sort. You can use < (less than), >
(greater than), <= (less than or equal to), >= (greater than or equal to), != (not equal to), == (equivalent
to). At its simplest, the expression can use a single Boolean type variable ( someBooleanVariable ).
If you want the condition to be a false value of a Boolean variable, you can put a “not” in front of it
( !aBooleanVariable ), which is the exclamation point.
For the message, you will want to check for a legs value equivalent to 1 . If that condition is met, you
may have to do more than one thing. To do that, you can put all of the instructions inside their own
“shopping bag,” a set of curly brackets. You can do the same for the else clause.
1.
Replace the line with
if (legs == 1) {
Debug.Log("The " + gameObject.name + " has " + legs + " leg.");
}
else {
Debug.Log("The " + gameObject.name + " has " + legs + " legs.");
}
2.
Save the script, and click Play.
This time the message is grammatically correct for any number of legs.
One of the most common tasks carried out in the Start function is the initialization of variables,
particularly the types than cannot be initialized before the game has begun. Let's take the
opportunity to try setting and also initializing a variable in the Start function. Setting a non-Unity
type variable is easy.
1.
Select the code you added in the last section.
2.
Right-click and choose Toggle Line Comment(s) (Figure 5-31 ).
 
Search WWH ::




Custom Search