Enum comparison C#
Can anyone explain why this is not working?
void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag ("Boundary")) {
Debug.Log("Before " + direction);
if ((int)direction == (int)Direction.RIGHT)
direction = Direction.LEFT;
if ((int)direction == (int)Direction.LEFT)
direction = Direction.RIGHT;
Debug.Log("After " + direction);
}
}
This is what I am getting on the Console
console.png
(17.5 kB)
Comment
Answer by gjf · Feb 19, 2016 at 07:07 PM
it's doing exactly what you told it to.
you need to use an else if
instead of the second if
because after you set it to left, you're immediately setting it back to right.
there's no need to cast them to int
for the comparison if they're both the same type.
Your answer
Follow this Question
Related Questions
Unity3d select random enum in another script 1 Answer
C# missingReferenceException when comparing enums 0 Answers
My public enum isn't functioning like I want it to 0 Answers
Problem assigning enum value 1 Answer
I Keep Getting Error CS0052 1 Answer