Game Development Reference
In-Depth Information
One of the most common mistakes of beginner programmers is using = instead of == in a conditional.
Note
Wait—what if the sky is clear but it's nighttime? I shouldn't go outside to play in the dark. Under your
first variable declaration, add a second:
var sunIsUp: boolean = true;
Now I want to say, “If the sky is clear and the sun is up, then I will go out and play.” Edit your
conditional if statement to the following:
if (skyIsClear == true && sunIsUp == true) {
&& is the logic operator AND. Now both conditions must be met for the code inside the statement
block to execute.
The logic operator OR is represented as || , by pressing Shift+\ twice on a Mac. OR means exactly
what it sounds like: if one condition or the other is met, but not necessarily both, then the statement
block executes.
If you want different blocks of code to execute under certain conditions, you can use more than one
if statement:
function Start () {
if (sunIsUp) {
Debug.Log("I will get out of bed");
}
if (skyIsClear) {
Debug.Log("I do not need an umbrella");
}
}
Since both variables are set to true , when you run your script you see both statements appear in
the console.
In this example, if the sun is not up and the sky is not clear then nothing happens. When you want
an alternative block of code to execute if none of the previous conditions are met, you can use else :
function Start () {
if (sunIsUp) {
Debug.Log("I will get out of bed");
}
if (skyIsClear) {
Debug.Log("I do not need an umbrella");
}
else {
Debug.Log("I am staying in bed");
}
}
 
Search WWH ::




Custom Search