I have a row of 5 blue cubes. I trigger the centre cube to check to see if the cube to its left is also blue. If it is, I trigger that cube to see if its cube to the left is also blue. This works in Unity. Now, I comment out this code and do the same but for the right, everything works fine.
However, my issue:
If I trigger looking left logic followed by the looking right logic this causes a stack overflow exception. Since there is a limit of 5 cubes, there should be no overflow as it knows it needs only check 2 cubes either side (what my code knows) . Is there something I am missing or is this an limitation of Unity?
You probably made some infinite loop. Provide a code.
public void CheckNeighbours(Piece[,] board, int rowHit, int columnHit, PieceColour targetColour)
{
//LEFT
if (columnHit > 0 && board[rowHit, columnHit - 1].currentPieceColour == targetColour)
{
board[rowHit, columnHit - 1].CheckNeighbours(board, rowHit, columnHit - 1, targetColour);
}
//RIGHT
if (columnHit < 9 && board[rowHit, columnHit + 1].currentPieceColour == targetColour)
{
board[rowHit, columnHit + 1].CheckNeighbours(board, rowHit, columnHit + 1, targetColour);
}
HidePiece();
}
So the board is a multi dimensional array, the centre is the column Hit and the row is always 0 in my example. If i comment out one of the “CheckNeighbours” lines of code, it works for one side. However if I do both it causes a StackOverflowException: The requested operation caused a stack overflow.
@MarekRimal