How to use Enum?

Hi all,

I’ve been searching around, and I haven’t been able to find any guides on using enums in Unity.

I was wondering if someone could show me how to use them in an ‘if’ statement.

This is my code so far:

#pragma strict
var moveSpeed = 5;
enum Direction {up,down,left,right,none};
var moveDirection : Direction;

function Update () {
	if (moveDirection = moveDirection.up) {
		transform.eulerAngles = Vector3.zero;
		MoveForward();
	}
	if (moveDirection = moveDirection.down) {
		transform.eulerAngles = Vector3.zero;
		transform.rotation.z = 90;
		MoveForward();
	}
	if (moveDirection = moveDirection.left) {
		transform.eulerAngles = Vector3.zero;
		transform.rotation.z = 180;
		MoveForward();
	}
	if (moveDirection = moveDirection.right) {
		transform.eulerAngles = Vector3.zero;
		transform.rotation.z = 270;
		MoveForward();
	}
}

function MoveForward () {
	transform.Translate(Vector3.forward);
}

And I get 3 errors:

Assets/Scripts/MyGameScripts/Gameplay/Movement/PlayerMovementController.js(22,27): BCE0044: expecting ), found '='.

Assets/Scripts/MyGameScripts/Gameplay/Movement/PlayerMovementController.js(22,45): BCE0043: Unexpected token: ).

Assets/Scripts/MyGameScripts/Gameplay/Movement/PlayerMovementController.js(23,39): BCE0044: expecting :, found '='.

How can I fix this, and so you know any guides on using enums? Thanks!

First of all, if you want to compare then you should use operator == and NOT the assignment operator. Then the enum values don’t belong to the variable, but to the object. So you can rewrite it as following:

if (moveDirection == Direction.up) {
    // ...
}