Array is empty even though it is initialized in Start()

Currently I am making a chess game, and I have an issue where I want to call Highlight() in BoardController, but it is empty when I call it, even though I have instantiated it in the Start() method.

This is my BoardController class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoardController : MonoBehaviour
{
    [SerializeField] private Piece[] positions;
    [SerializeField] private GameObject[] blueHighlight;
    [SerializeField] private GameObject highlightSquare;
    void Start()
    {
        InstantiatePieces();
    }

    private void InstantiatePieces()
    {
        blueHighlight = new GameObject[64];

        for (int i = 0; i < 64; i++)
        {
            int x = i % 8;
            int y = i / 8;

            blueHighlight[i] = Instantiate(highlightSquare, new Vector3(x, y, 0), Quaternion.identity);

            if (positions[i] != null)
            {
                Instantiate(positions[i], new Vector3(x, y, 0), Quaternion.identity);
            }
        }
    }

    private void Update()
    {
       
    }

    public bool IsLegalMove(int x, int y)
    {
        int pos = y * 8 + x;
        if (positions[pos] != null || !IsInBounds(x, y))
        {
            // change based on black or white;
            return false;
        }

        return true;
    }

    public bool IsInBounds(int x, int y)
    {
        if (x < 0 || x > 7 || y < 0 || y > 7)
        {
            return false;
        }

        return true;
    }

    public void Highlight(int x, int y)
    {
        Debug.Log("Highlight");
        int pos = ConvertToPos(x, y);
        blueHighlight[pos].SetActive(true);
        Debug.Log("Completed");
    }

    // Converts (x, y) coords to 0 - 63
    public int ConvertToPos(int x, int y)
    {
        return y * 8 + x;
    }

    public int[] ConvertToXY(int pos)
    {
        return new int[] { pos % 8, pos / 8 };
    }
}

And this is the Rook class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Rook : Piece
{
    public int[,] delta = new int[1, 2] {{0, 1}};

    private void Start()
    {
        base.Start();
    }

    public override void GetAvailableMoves()
    {
        for (int i = 0; i < delta.Length - 1; i++)
        {
            int x = delta[i, 0];
            int y = delta[i, 1];
            if (boardController.IsLegalMove(x, y))
            {
                Debug.Log("Legal");
                boardController.Highlight(x, y);
            }
        }

    }
}

What I wanted to do is that when I click on the Rook piece, it will highlight the square it is sitting on, but I get an IndexOutOfBounds error since the blueHighlight array has length of 0 for some reason, even though I initialized it at the start

What is “boardController” inside your Rook class? It’s probably a field that comes from the “Piece” class I guess? What and where did you assign something to that variable? My guess is that you may have assigned a prefab to that variable but you never assigned the actual instance in the scene to that variable.

You can verify which object you’re referencing by using a Debug.Log statement and pass the object you want to know as a context argument. Like this:

Debug.Log("Rook's boardController", boardController);

When you click on that log message, the Unity editor will highlight / ping the passed context object in the hierarchy / project. So you know if you’re dealing with the correct object. The same thing can be checked at runtime by simply clicking on the “boardController” object field of your rook instance. This should also highlight the referenced object. As I said, my guess is that the object is not the object you have in mind.

We don’t know how your pieces get instantiated. Though you probably want to assign the board controller when you create the pieces, or use a singleton instead. A singleton is a cheap workaround. Actually assigning the reference when you instantiate the pieces would allow you to create several boards in your game and each has its own board controller. You probably don’t need this, but it shows the difference in flexibility.