Unknown Identifier in Finite State Machine

Hey all, I'm having trouble implementing a Finite state machine and every piece of documentation I've seen seems deliberately vague. Basically, my code's turning up an Unknown Identifier error in lines 19, 21, 23, and 25(case X). Can someone explain to me why this error is appearing and how to rectify it? Also, how do I go about changing the state from within the functions called from Update?

var speed = 10;
var minspeed = 2.5;
speed *= Random.value;
speed += minspeed;
var shipTarget : Transform;

enum state {
    WAITING,
    SEEKING,
    ATTACKING,
    MOVING
}

function Update () {
    if (Time.timeScale > 0) {
        if (transform.position.y == 0) {
            switch (state) 
            {
            case WAITING:
                shipWait(5);
            case SEEKING:
                facePlayer(shipTarget);
            case ATTACKING:
                fire();
            case MOVING:
                moveForward();
            }

        }
    }
}

function shipWait (waitTime : float) { //Waits up to 5 seconds.
    yield WaitForSeconds(Random.value * waitTime);
}

function moveForward () { //Simply moves forward
transform.Translate (0, 0, speed * Time.deltaTime);
}

function facePlayer (target : Transform) { //Turns to face the player
        if (shipTarget) {
        // Look at and dampen the rotation
        var rotation = Quaternion.LookRotation(shipTarget.position - transform.position);
        transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime);
    }
}

function fire () {
    gameObject.SendMessage("Shoot", SendMessageOptions.DontRequireReceiver);
}

2 errors.

  • You're trying to switch on a type (state is a type, not a variable).
  • You must include the enum type in your case switches. It is not sufficient to write "WAITING". It should be "state.WAITING". In other languages like C++ you dont write the enumeration type strongly like in C#/JS.

You probably also should use break; for each of your cases or your code would "fall through" the next case(s).

enum State {                    // Renamed type state to State.
    WAITING,
    SEEKING,
    ATTACKING,
    MOVING
}

var state : State;              // Need to declare a variable.

function Update () {
    if (Time.timeScale > 0) {
        if (transform.position.y == 0) {
            switch (state) 
            {
            case State.WAITING: // Need to be written ENUMTYPE.ENUMMEMBER
                shipWait(5);
                break;          // Probably want to break.
            case State.SEEKING:
                facePlayer(shipTarget);
                break;
            case State.ATTACKING:
                fire();
                break;
            case State.MOVING:
                moveForward();
                break;
            }

        }
    }
}

You need a break statement I believe, that's why they don't let us use switch statements in college... lol.