I’m using instantiation to set up a chess board. I’ve managed to get the board itself to work but cannot do the same for the pieces:
Code containing the error:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Board : MonoBehaviour
{
//Assigning GameObjects and Sprites for instantiation
public GameObject CellPrefab;
public Sprite GreenCell;
public Sprite YellowCell;
public GameObject PawnPrefab, KnightPrefab, BishopPrefab, RookPrefab, QueenPrefab, KingPrefab;
public Sprite WhitePawn, WhiteKnight, WhiteBishop, WhiteRook, WhiteQueen, WhiteKing;
public Sprite BlackPawn, BlackKnight, BlackBishop, BlackRook, BlackQueen, BlackKing;
public Material whitePieceMat, blackPieceMat;
public GameObject[,] Squares = new GameObject[8,8];
//This is a list for the columns on the board
[HideInInspector]
public static string[] alphabet = new string[] {"A", "B", "C", "D", "E", "F", "G", "H"};
//This function is here to create a delay, so that each of the squares on the board is drawn one by one
//Beacuse it makes a cool effect.
public void CreateBoard()
{
StartCoroutine(DrawBoard());
SetupPieces();
}
//This is the function that actually draws the board
IEnumerator DrawBoard()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
yield return 0;
Squares[i, j] = Instantiate(CellPrefab, new Vector3(i, j, 0), Quaternion.identity);
//Black Squares have must either have both X and Y coordinates even or both odd
if (i % 2 != 0 && j % 2 != 0 || i % 2 == 0 && j % 2 == 0)
{
Squares[i, j].GetComponent<SpriteRenderer>().sprite = GreenCell;
}
else
{
Squares[i, j].GetComponent<SpriteRenderer>().sprite = YellowCell;
}
//These lines give each cell a unique letter and number between "A1" and "H8" so that I can distinguish them later
Squares[i, j].transform.SetParent(gameObject.transform);
Squares[i, j].name = alphabet[i] + (j + 1);
}
}
}
GameObject[] PieceArrangement;
public void SetupPieces()
{
PieceArrangement = new GameObject[8]
{
RookPrefab, // R = 0
KnightPrefab, // N = 1
BishopPrefab, // B = 2
QueenPrefab, // Q = 3
KingPrefab, // K = 4
BishopPrefab, // B = 5
KnightPrefab, // N = 6
RookPrefab // R = 7
};
for (int i = 0; i < 8; i++)
{
// SETUP 1st RANK WHITE PIECES
GameObject WhiteNew = Instantiate(PieceArrangement[i], Squares[i, 0].transform);
WhiteNew.gameObject.GetComponent<Piece>().white = true;
WhiteNew.GetComponent<Renderer>().material = whitePieceMat;
// SETUP 2nd RANK WHITE PAWNS
GameObject PWhiteNew = (GameObject)Instantiate(PawnPrefab, Squares[i, 1].transform);
PWhiteNew.gameObject.GetComponent<Piece>().white = true;
PWhiteNew.GetComponent<SpriteRenderer>().sprite = WhitePawn;
// SETUP 7th RANK BLACK PAWNS
GameObject PBlackNew = (GameObject)Instantiate(PawnPrefab, Squares[i, 6].transform);
PBlackNew.gameObject.GetComponent<Piece>().white = false;
PBlackNew.GetComponent<SpriteRenderer>().sprite = BlackPawn;
// SETUP 8th RANK BLACK PIECES
GameObject BlackNew = (GameObject)Instantiate(PieceArrangement[i], Squares[i, 7].transform);
BlackNew.gameObject.GetComponent<Piece>().white = false;
BlackNew.GetComponent<Renderer>().material = blackPieceMat;
}
}
public void Rotate()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Squares[i, j].gameObject.transform.localPosition = new Vector3(
7 - Squares[i, j].gameObject.transform.localPosition.x,
7 - Squares[i, j].gameObject.transform.localPosition.y,
0);
}
}
print("Board rotated");
}
}
Game Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : MonoBehaviour
{
Board board;
void Start()
{
board = gameObject.GetComponent<Board>();
board.CreateBoard();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
board.Rotate();
}
}
}