- Home /
Switch-case with enum
Can you use switch-case with an enum in Unity? If so, how do you format the conditional check?
FYI, the snippet below does NOT work (with ""'s around the conditional check, I get the cannot == error, as the enum is not a string) . As it is, I get the unknown identifier error.
enum choices{ APPLE, banana, canaple } function Start(){
var choice:choices=APPLE; switch(choice){ case APPLE: Debug.Log("APPLE"); break;
case banana: Debug.Log("B"); break;
case canaple: Debug.Log("CC"); break;
default: Debug.Log("nothing"); break; }
}
Answer by Ejlersen · Jan 03, 2011 at 05:56 PM
Yes, you can. Just modifying your code a bit :)
enum choices { APPLE, BANANA, CANAPLE }
function Start() { var choice : choices = choices.APPLE;
switch(choice)
{
case choices.APPLE:
Debug.Log("APPLE");
break;
case choices.BANANA:
Debug.Log("BANANA");
break;
case choices.CANAPLE:
Debug.Log("CANAPLE");
break;
default:
Debug.Log("NOTHING");
break;
}
}
ahhhhh! simply have to use dot notation. thanks! :)
(and sorry for the quick code and no formatting above - was just quick check and found it did not work..)
Your answer
Follow this Question
Related Questions
What is a more efficient way to write this Switch Statement? 3 Answers
Problem with my weapons switch statement C# 3 Answers
Enum Method vs Switch 1 Answer
Problems with enums and switches (C#) 2 Answers