My Script Won't Let Me Castle C# Chess
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BoardManager : MonoBehaviour {
public bool[] castlingFlags { set; get;}
public static BoardManager Instance{ set; get; }
private bool[,] allowedMoves{ set; get; }
public Chessman[,] Chessmans{ set; get; }
private Chessman selectedChessman;
private const float TileSize = 1.0f;
private const float TileOffset = 0.5f;
private int selectionX = -1;
private int selectionY = -1;
public List<GameObject> chessmanPrefabs;
private List<GameObject> activeChessman = new List<GameObject>();
public int[] EnPassantMove{ set; get; }
public bool isWhiteTurn = true;
private void Start()
{
Instance = this;
SpawnAllChessmans ();
}
private void Update()
{
UpdateSelection ();
DrawChessBoard ();
if (Input.GetMouseButtonDown(0))
{
if (selectionX >= 0 && selectionY >= 0)
{
if (selectedChessman == null)
{
//S elect the chessman
SelectChessman(selectionX, selectionY);
}
else
{
//Move the chessman
MoveChessman(selectionX, selectionY);
}
}
}
}
private void SelectChessman(int x, int y)
{
if (Chessmans [x, y] == null)
return;
if (Chessmans [x, y].isWhite != isWhiteTurn)
return;
bool hasAtleastOneMove = false;
allowedMoves = Chessmans [x, y].PossibleMove ();
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (allowedMoves [i, j])
hasAtleastOneMove = true;
selectedChessman = Chessmans [x, y];
BoardHighlights.Instance.HighlightAllowedMoves (allowedMoves);
}
private void MoveChessman(int x, int y)
{
if (allowedMoves[x,y])
{
Chessman c = Chessmans [x, y];
if(c != null && c.isWhite != isWhiteTurn)
{
// Capture a Piece
//If it is the king
if(c != null && c.GetType() == typeof(King))
{
EndGame ();
return;
}
if (c != null && c.GetType() == typeof(Rook))
{
if (!isWhiteTurn)
{
if (x == 7 && y == 0)
castlingFlags[0] = true;
else if (x == 0 && y == 0)
castlingFlags[1] = true;
}
else
{
if (x == 7 && y == 7)
castlingFlags[3] = true;
else if (x == 0 && y == 7)
castlingFlags[4] = true;
}
}
activeChessman.Remove(c.gameObject);
Destroy (c.gameObject);
}
if (x == EnPassantMove [0] && y == EnPassantMove [1])
{
if (isWhiteTurn)
{
c = Chessmans [x,y - 1];
activeChessman.Remove(c.gameObject);
Destroy (c.gameObject);
} else
{
c = Chessmans [x, y + 1];
activeChessman.Remove(c.gameObject);
Destroy (c.gameObject);
}
}
EnPassantMove [0] = -1;
EnPassantMove [1] = -1;
if (selectedChessman.GetType () == typeof(Pawn))
{
if (selectedChessman.CurrentY == 1 && y == 3)
{
EnPassantMove [0] = x;
EnPassantMove [1] = y - 1;
}
else if (selectedChessman.CurrentY == 6 && y == 4)
{
EnPassantMove [0] = x;
EnPassantMove [1] = y + 1;
}
}
Castling(x, y);
Chessmans [selectedChessman.CurrentX, selectedChessman.CurrentY] = null;
selectedChessman.transform.position = GetTileCenter (x, y);
selectedChessman.SetPosition (x, y);
Chessmans [x, y] = selectedChessman;
isWhiteTurn = !isWhiteTurn;
}
BoardHighlights.Instance.HideHighlights ();
selectedChessman = null;
}
private void UpdateSelection()
{
if (!Camera.main)
return;
RaycastHit hit;
if (Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 25.0f, LayerMask.GetMask ("ChessPlane"))) {
selectionX = (int)hit.point.x;
selectionY = (int)hit.point.z;
} else
{
selectionX = -1;
selectionY = -1;
}
}
private void SpawnChessman(int index, int x, int y)
{
GameObject go = Instantiate (chessmanPrefabs [index], GetTileCenter(x,y), Quaternion.identity) as GameObject;
go.transform.SetParent (transform);
Chessmans [x, y] = go.GetComponent<Chessman> ();
Chessmans [x, y].SetPosition (x, y);
activeChessman.Add (go);
}
private void SpawnAllChessmans()
{
activeChessman = new List<GameObject>();
Chessmans = new Chessman[8, 8];
EnPassantMove = new int[2]{-1,-1};
// Spawn the White Team!
// King
SpawnChessman (0,4,0);
// Queen
SpawnChessman (1,3,0);
// Rooks
SpawnChessman (2,0,0);
SpawnChessman (2,7,0);
// Bishops
SpawnChessman (3,2,0);
SpawnChessman (3,5,0);
// Knights
SpawnChessman (4,1,0);
SpawnChessman (4,6,0);
// Pawns
for(int i = 0; i < 8; i++)
SpawnChessman (5,i,1);
// Spawn the Black Team!
// King
SpawnChessman (6,4,7);
// Queen
SpawnChessman (7,3,7);
// Rooks
SpawnChessman (8,0,7);
SpawnChessman (8,7,7);
// Bishops
SpawnChessman (9,2,7);
SpawnChessman (9,5,7);
// Knights
SpawnChessman (10,1,7);
SpawnChessman (10,6,7);
// Pawns
for(int i = 0; i < 8; i++)
SpawnChessman (11,i,6);
}
private Vector3 GetTileCenter (int x, int z)
{
Vector3 origin = Vector3.zero;
origin.x += (TileSize * x) + TileOffset;
origin.z += (TileSize * z) + TileOffset;
return origin;
}
private void DrawChessBoard()
{
Vector3 widthLine = Vector3.right * 8;
Vector3 heightLine = Vector3.forward * 8;
for (int i = 0; i <= 8; i++)
{
Vector3 start = Vector3.forward * i;
Debug.DrawLine (start, start + widthLine);
for (int j = 0; j <= 8; j++)
{
start = Vector3.right * j;
Debug.DrawLine (start, start + heightLine);
}
}
// Draw the selection
if (selectionX >= 0 && selectionY >= 0)
{
Debug.DrawLine(
Vector3.forward * selectionY + Vector3.right * selectionX,
Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
Debug.DrawLine(
Vector3.forward * (selectionY + 1)+ Vector3.right * selectionX,
Vector3.forward * selectionY + Vector3.right * (selectionX + 1));
}
}
private void EndGame()
{
if (isWhiteTurn)
Debug.Log ("White Team Wins!");
else
Debug.Log ("Black Team Wins!");
foreach (GameObject go in activeChessman)
Destroy (go);
isWhiteTurn = true;
BoardHighlights.Instance.HideHighlights ();
SpawnAllChessmans();
}
private void Castling (int x, int y)
{
if (selectedChessman.GetType() == typeof(King) || selectedChessman.GetType() == typeof(Rook))
{
if (isWhiteTurn)
{
if (selectedChessman.CurrentX== 7 && selectedChessman.CurrentY== 0)
castlingFlags[0] = true;
else if (selectedChessman.CurrentX== 0 && selectedChessman.CurrentY== 0)
castlingFlags[1] = true;
else if (selectedChessman.CurrentX== 5 && selectedChessman.CurrentY== 0)
castlingFlags[2] = true;
}
else
{
if (selectedChessman.CurrentX== 7 && selectedChessman.CurrentY== 7)
castlingFlags[3] = true;
else if (selectedChessman.CurrentX== 0 && selectedChessman.CurrentY== 7)
castlingFlags[4] = true;
else if (selectedChessman.CurrentX== 5 && selectedChessman.CurrentY== 7)
castlingFlags[5] = true;
}
Chessmans[x, y] = selectedChessman;
}
}
}
Is the script. I get a few errors depending on what I try to move on the board.
NullReferenceException: Object reference not set to an instance of an object BoardManager.Castling (Int32 x, Int32 y) (at Assets/Scripts/BoardManager.cs:305) BoardManager.MoveChessman (Int32 x, Int32 y) (at Assets/Scripts/BoardManager.cs:150) BoardManager.Update () (at Assets/Scripts/BoardManager.cs:52)
NullReferenceException: Object reference not set to an instance of an object BoardManager.Castling (Int32 x, Int32 y) (at Assets/Scripts/BoardManager.cs:301) BoardManager.MoveChessman (Int32 x, Int32 y) (at Assets/Scripts/BoardManager.cs:150) BoardManager.Update () (at Assets/Scripts/BoardManager.cs:52)
Your answer
Follow this Question
Related Questions
Assigning to array give NullReferenceExecption!?!! 0 Answers
Can't access public static array from another class 1 Answer
AI to shoot other AI 1 Answer
Named array assigned to new array, named array returns null on all subsequent calls 0 Answers
Null reference exception in an if statement that checks for it. 1 Answer