Array out of range, should be fine as far as I can tell..

I’m getting an out of range error almost every time the last line is called when locationX & locationY are at the edge of the array.

gameSpaces is a 5x5 array of GameObjects. 0,0 is the bottom left position, 4,4 is the top right.

			switch(walkdirection){

			case WalkDirection.Up:
				if(locationY < gameController.gameSpaces.GetLength(1)){
					locationY++;
				}
				break;
				
			case WalkDirection.Down:
				if(locationY > 0){
					locationY--;
				}
				break;
				
			case WalkDirection.Left:
				if(locationX > 0){
					locationX--;
				}
				break;
				
			case WalkDirection.Right:
				if(locationX < gameController.gameSpaces.GetLength(0)){
					locationX++;
				}
				break;
			}

			transform.position = gameController.gameSpaces[locationX, locationY].transform.position;

This can’t work:

if(locationY < gameController.gameSpaces.GetLength(1)){
      locationY++;
}

Because if locationY is equal to GetLength(1)-1, and you do locationY++, then it will equal GetLength(1), which is out of range. Same for locationX++.