- Home /
Question by
AlfAtUnity · Jul 08, 2013 at 05:33 PM ·
javascripterrorenum
[CLOSED] UnityScript - enum to numeric value error
I get error: The type 'int' does not have a visible constructor that matches the argument list '(States)'. But I don't see the error - it's explicit conversion from enum to int.
enum States
{
IDLE, FALLING, FLYING
}
var listStates : State[] = new State[NUMBER_OF_STATES];
function initialize() : void
{
//init list of states
listStates[int(States.IDLE)] = new IdleState(); // ERROR
listStates[int(States.FALLING)] = new FallingState(); // ERROR
listStates[int(States.FLYING)] = new FlyingState(); // ERROR
}
Why this error message?
Comment
The problem is the syntax you use : it is for constructors. Look at my answer.
Best Answer
Answer by KiraSensei · Jul 08, 2013 at 05:39 PM
Try :
function initialize() : void
{
//init list of states
listStates[States.IDLE] = new IdleState();
listStates[States.FALLING] = new FallingState();
listStates[States.FLYING] = new FlyingState();
}
Or if you really want an explicite cast :
function initialize() : void
{
//init list of states
listStates[(int)States.IDLE] = new IdleState();
listStates[(int)States.FALLING] = new FallingState();
listStates[(int)States.FLYING] = new FlyingState();
}
(int)enum_value => it's for C#; in UnityScript => int(enum_value). Using listState[enum_value] => will create new property w/ enum_value. I want to use numeric index. How do you think - using enum as array index (= object property) will affect performance OR not?