- Home /
Enum and Variable with similar name resulting in error
Hey, I'm making a Claw Game. I have a fbx baseclaw mesh with script (with movement, inputs and states (and a kinematic rigidbody)) added and a fbx for the claw itself with the animations added.
Here's the code:
#pragma strict
public var clawSpeed : float = 2;
private var moveDirection : Vector3;
private var myTransform : Transform;
enum CharacterState
{
Idle,
Opening,
Lowering,
Closing,
Closed,
Raising,
RaisedClosed,
OpeningAgain,
}
var _characterState : CharacterState;
function Start ()
{
myTransform = transform;
//_characterState = CharacterState.Idle;
}
function Update ()
{
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.z = Input.GetAxis("Vertical");
moveDirection.Normalize();
myTransform.position += moveDirection * clawSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.X))
{
switch (_characterState.Idle)
{
case _characterState.Opening:
//play animation
animation.Play("Opening");
_characterState = CharacterState.Lowering;
//change state
break;
case _characterState.Lowering:
//play animation
animation.Play("Lowering");
_characterState = CharacterState.Closing;
//change state
break;
case _characterState.Closing:
//play animation
animation.Play("Closing");
_characterState = CharacterState.Closed;
//change state
break;
}
}
}
Here's the error message: "BCE0132: The namespace '' already contains a definition for 'CharacterState'."
Objective: When X button is pressed one time plays "Opening" Animation, X button again plays "Lowering" Animation, X button again plays "Closing" and it keep going on.
Please help me!!
Answer by KellyThomas · Jan 27, 2014 at 03:06 PM
Well the error is correct: in this context the names must be unique.
This means you have to rename either this enum or the other CharacterState.
As I usually pluralise the names of my enums I would do this:
#pragma strict
public var clawSpeed : float = 2;
private var moveDirection : Vector3;
private var myTransform : Transform;
enum CharacterStates
{
Idle,
Opening,
Lowering,
Closing,
Closed,
Raising,
RaisedClosed,
OpeningAgain,
}
var _characterState : CharacterStates;
function Start ()
{
myTransform = transform;
//_characterState = CharacterStates.Idle;
}
function Update ()
{
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.z = Input.GetAxis("Vertical");
moveDirection.Normalize();
myTransform.position += moveDirection * clawSpeed * Time.deltaTime;
if (Input.GetKey(KeyCode.X))
{
switch (_characterState.Idle)
{
case _characterState.Opening:
//play animation
animation.Play("Opening");
_characterState = CharacterStates.Lowering;
//change state
break;
case _characterState.Lowering:
//play animation
animation.Play("Lowering");
_characterState = CharacterStates.Closing;
//change state
break;
case _characterState.Closing:
//play animation
animation.Play("Closing");
_characterState = CharacterStates.Closed;
//change state
break;
}
}
}
Thanks $$anonymous$$elly$$anonymous$$,
It really worked. The error is gone. But do you have an idea why it isn't playing any of the animations? I'm pressing X and it's not playing none of the animations
Hmm.. I don't look any further than the error you described in your question, but on closer inspection I'm surprised it complied without any warnings.
1. Try making this change:
//switch (_characterState.Idle)
switch (_characterState)
2. Do you have other code that will set the value of _characterState
? This switch will only have an effect if _characterState
is of one of the supported values!
Well...it kinda worked. But I got other problems haha I'll post on another thread! Thanks, dude!
$$anonymous$$eep in $$anonymous$$d that this should have been a comment, not an answer.
Your answer
Follow this Question
Related Questions
What is a more efficient way to write this Switch Statement? 3 Answers
How to check if an enum’s case has changed 3 Answers
Enum Method vs Switch 1 Answer
Problems with enums and switches (C#) 2 Answers
Operator '==' cannot be used! 2 Answers