- Home /
Enum switch statement error
enum GameType { Survival, Escape, Prevail }
var game = GameType.Survival;
In custom Editor script:
switch (target.game)
{
case GameType.Survival:
Debug.Log("hello");
}
Error: Operator '==' cannot be used with a left hand side of type 'System.Object' and a right hand side of type 'GameType'.
this is at the top of the script:
@CustomEditor (LevelData)
class LevelData_CustomInspector extends Editor
{
It isn't defined. I've been using target.[member] in many other ways just fine up until the switch function. I think it's possibly a bug just localized to the switch function.
Anyways, I posted an answer that got around it.
Answer by Essential · Nov 26, 2012 at 06:00 PM
Okay I figured it out… For some reason the switch function doesn't properly understand the type when referencing from another script (?). So it needs to be specifically cast before it can be used. Very strange. Anyway… this fixed it:
var currentGameType : GameType = currentArea.game;
switch (currentGameType)
{
case GameType.Survival:
Debug.Log("hello");
}
This isn't actually the correct answer; you have something else going on. For example, this works fine...if you have this script, called Example.js:
enum GameType { Survival, Escape, Prevail }
var game = GameType.Survival;
And this script, called Example2.js:
var target : Example;
function Start () {
switch (target.game) {
case GameType.Survival:
Debug.Log("hello");
break;
}
}
It works with no issues.
This is why I was asking how you are defining "target". Because it's apparently an Object, which won't work.
Hmm… I might have it wrong but I thought pointing at another script used "target" as a reference to that gameObject's script.
I haven't created it manually, and so I wouldn't know how to define it as anything else.
Answer by KiraSensei · Nov 26, 2012 at 05:36 PM
Try to declare your var game as a GameType (still an enum, but the compiler knows that it is actually an int).
var game:GameType = GameType.Survival;
Thanks but it's already defined as GameType, I just missed that out from my example code above. :)
Doing
var game:GameType = GameType.Survival;
is 100% identical to
var game = GameType.Survival;
Since the compiler sees that you are assigning a GameType to the variable, it's statically typed as GameType whether you specify the type or not.
Your answer
Follow this Question
Related Questions
BCE0051 Error 1 Answer
Enum is thought to be a float? 2 Answers
error BCE0022: Cannot convert error, please help 3 Answers
Switch case using enum javascript 2 Answers
hashtable: snapshot out of sync 2 Answers