- Home /
Question by
Yharooer · Jul 11, 2013 at 10:08 AM ·
enumifsyntax-error
Using Enum in an if Statement
If I do the following:
#pragma strict
enum direction {up,down,left,right,none};
function Update () {
if (direction == up) {
transform.Translate(Vector3.forward);
}
}
Then I get an error: Assets/Test.js(6,26): BCE0005: Unknown identifier: 'up'.
How can I fix this?
Comment
Best Answer
Answer by shaderop · Jul 11, 2013 at 10:20 AM
enum
defines a type, not a variable. What you have in your code is an enum
type named direction
, but you don't have any instances (i.e. variables) of that type to compare against.
Try this instead:
#pragma strict
// It's good form to start enum names with a capital letter
enum Direction { up, down, left, right };
// Declare the variable and initialize it
var direction:Direction = up;
function Update() {
if (direction == Direction.up)
{
// Go to town
}
}
Now I get:
%location%(22,27): BCE0044: expecting ), found '='.
%location%(22,45): BCE0043: Unexpected token: ).
%location%(23,39): BCE0044: expecting :, found '='.
#pragma strict
enum Direction { up, down, left, right };
var direction = Direction.up;
function Update() {
if (direction == Direction.up)
{
//Do whatever you want
}
}