I have the following code:
private bool CheckForCheckedKing(GameObject Item1, GameObject Item2)
{
// Method for seeing if the user will put themselves in check or not BEFORE the move is performed
Player1Turn = !Player1Turn; // Turn order needs to be switched temporarily for the correct set of pieces to be compared when Check() is called
bool KingInDanger = false;
Vector2 Item1Position = Item1.transform.position;
Vector2 Item2Position = Item2.transform.position;
Item1.transform.position = Item2Position; // Temporarily swaps the pieces
Item2.transform.position = Item1Position;
if (Item2.tag != "EmptySquare")
Item2.SetActive(false); // if item2 was an enemy piece, needs to disable it as if the piece was taken
if (Player1Turn == false) //As the turn order is flipped in this function, false means that player 1 is trying to move so needs to check their king
{
GameObject TargetKing = GameObject.Find("WhiteKing(Clone)");
KingInDanger = Check(TargetKing);
}
else
{
GameObject TargetKing = GameObject.Find("BlackKing(Clone)");
KingInDanger = Check(TargetKing);
}
Item2.SetActive(true);
Item1.transform.position = Item1Position;
Item2.transform.position = Item2Position; // Restores the original positions and state of the pieces
Player1Turn = !Player1Turn;
return KingInDanger;
}
private void MovePiece(GameObject Item1, GameObject Item2)
{
Vector2 Item1Position = Item1.transform.position;
Vector2 Item2Position = Item2.transform.position;
Item1.transform.position = Item2Position;
EmptySquare.SetActive(true);
Instantiate(EmptySquare, Item1Position, Quaternion.identity, GameObject.FindGameObjectWithTag("ChessBoard").transform);
EmptySquare.SetActive(false);
Destroy(Item2);
// Code swaps the locations of items 1 and 2 and replaces item 2 with an empty square
}
They both involve swapping the positions of 2 gameobjects containing colliders before using another method (like Check()) to raycast from various other pieces. The issue here however, is that from debugging the code, it appears as though the gameobjects aren’t swapped until after the rays are casted. Once all of the code is performed, the gameobjects are then perfectly operational in their new position.
Does anyone know how I can get the gameobjects to swap properly before the rays are casted?