Search code examples
c#xna

"X" does not exist in this context


I don't understand the logic behind this error in the second section.

The name 'newMove' does not exist in the current context.

Isn't it define just above in the first section?

            //First section
            if (base.IsPlayer1 == true)
            {
                Move newMove = moveList.Detect();
            }
            else if (base.IsPlayer1 == false)
            {
                MoveKeyboard newMove = moveListKeyboard.DetectMove();
            }

            //Second section
            if (newMove != null)
            {
                 PlayMove();
            }

Thanks.


Solution

  • What you're seeing is a scope problem. newMove is defined within the if or else if statement and is only accessible within whichever block created the variable.

    You could define it above, but it looks like you have two different Types (Move and MoveKeyboard). If you can consolidate those to the same type you could have a single definition above the first if statement.