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: Code (CSharp): 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: Code (CSharp): 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(); } } }
Look at the line the error is reporting. Debug variables in that line until you find something null. Figure out how to make that thing not null. In your case Code (csharp): WhiteNew.gameObject.GetComponent<Piece>().white = true; The only two things that can be null are 'WhiteNew' and the '<Piece>' component. If 'WhiteNew' exists, then there is no Piece component on it.
Thanks for the quick response, but the code hits the error before it reaches that line. I put a Debug.Log above that line and it didn't do anything, but it did above the previous line. So the error must be on the line above.
The easiest would be to step through using the debugger. Also, always try to make sure the lines of the source file and the snippets you post here match or tell us at least which line it actually is (inside the source file). Anyway, it seems the problem occurs due to your way of initializing everything. You use a coroutine to populate the squares array, but just right after starting the coroutine (which is going to execute everything in your code immediately until it hits the first yield statement), you call the setup method which does already access the square array's elements. So the coroutine will stop before it creates the first element for the square array and yields its control - the next part will be executed in the frame (as you yield 0 - which is basically saying "wait one frame"). StartCoroutine will now return and the CreateBoard method executes SetupPieces. Since the coroutine has not finished (and has not even created a single square/cell whatever you call it), you're about to access null entries in the following line: Code (CSharp): GameObject WhiteNew = Instantiate(PieceArrangement[i], Squares[i, 0].transform);
All null reference exceptions are debugged the same way. The way to debug them is so straightforward that some sites don't even allow posting questions about null ref exceptions. Find the line it says is the problem, log the variables until you find something null, use common sense and work backwards until you find out why it is null, then correct the issue. If you're using someone elses code and this is an error you are getting (likely) then you need to contact the author, not us.
Based on @Suddoha 's explanation, I'd suggest that you put "SetupPieces();" at the end of the coroutine. Also, change "yield return 0;" to "yield return null;"