Game Development Reference
In-Depth Information
Your variables are still set to
true
, so running your script doesn't do anything different, but you can
see there are many ways you can test a variety of conditions to direct your gameplay.
The
if
statement can compare numerical values as well. Other comparison operators (Table
2-2
)
you can use include
<
,
>
,
<=
,
>=
, and
!=. !
represents “not,” so
!=
is the comparison operator “is not
equal to.”
Table 2-2.
Definitions of Comparison Operators
Comparison Operators
Meaning
==
Equal to
!=
Not equal to
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
The
switch-case
conditional works in place of a chain of
if-else
statements for when you need to
compare a variable against different values.
Edit your script to the following:
#pragma strict
var diceRoll : float;
function Start () {
diceRoll = Random.Range(1, 6);
switch (diceRoll) {
case 1:
print ("You rolled a 1!");
break;
case 2:
print ("You rolled a 2!");
break;
case 3:
print ("You rolled a 3!");
break;
case 4:
print ("You rolled a 4!");
break;
case 5:
print ("You rolled a 5!");
break;
case 6:
print ("You rolled a 6!");
break;