1 year ago
#359633
PrOeZ
Return statement doesn't stop the method, C#, unity 2D
I'm working on a chess game and I'm trying to make a class that checks if a position is in check or not. Whithin that class I have a function that goes through squares whithin an array, to verify if that square is safe or not. Here's my code :
int x = xPosition;
int y = yPosition;
y += yIncriment;
x += xIncriment;
while (gameScript.PositionOnBoard(x, y) && gameScript.piecesArray[x, y] == null)
{
y += yIncriment;
x += xIncriment;
}
if(!gameScript.PositionOnBoard(x, y))
{
return false;
}
if(gameScript.piecesArray[x, y].GetComponent<PieceManager>().player == ps.player)
{
return false;
}
if(gameScript.piecesArray[x, y].GetComponent<PieceManager>().name == "queen" || gameScript.piecesArray[x, y].GetComponent<PieceManager>().name == "bishop")
{
return true;
}
return false;
The problem that i'm having right now is that my function does not stop when the position is no longer on the board. The bug occurs when my script tries to get a property of a null object, that's why i used the "return false" to stop the method, but it goes through it anyways.
c#
unity-game-engine
chess
0 Answers
Your Answer