2D Array not being initialized correctly, not persisting within class?

I’m trying to initialize a 2D array and have another GameObject use it. In my case, the 2D array i’ve created is GameObject[,] gameBoard. However, whenever the other class is referencing it, I keep getting a NullReferenceException.

public class GameBoard : MonoBehaviour {

    public const int ROWS = 6;
    public const int COLUMNS = 5;
    private GameObject[,] gameBoard;

    public static int numberOfDifferentPieces = 5;

    // Use this for initialization
    void Awake () {
        gameBoard = new GameObject[ROWS, COLUMNS];
    }

    // Update is called once per frame
    void Update () {
        // Testing
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log(gameBoard[0, 0].name);
        }
    }

    // Changes the gameBoard array to reflect game piece positions in game
    public void setGameBoardPosition(int row, int column, GameObject gamePiece)
    {
        gameBoard[row, column] = gamePiece;
        Debug.Log("Setting X: " + row + " Setting Y: " + column + " with GamePiece: " + gameBoard[row, column].name);
    }

The other class:

public class MovePiece : MonoBehaviour {
    void OnTriggerEnter(Collider other)
    {
        string colliderName = other.gameObject.name;

        // When a piece collides with a differnt tile on the gamebaord, set its lastPosition to the new tile
        // and change the array in GameBoard
        if (other.tag == "GameBoard")
        {
            lastPosition = other.gameObject;

            string[] coordinate = colliderName.Split('x');
            int row = Int32.Parse(coordinate[0]);
            int column = Int32.Parse(coordinate[1]);

            this.row = row;
            this.column = column;

            // Causes NullReferenceException
            gameBoard.setGameBoardPosition(row, column, this.gameObject);
        }
   
    }

I’ve also tried initializing the 2D array at the beginning of the class like this:

private GameObject[,] gameBoard = new GameObject[ROWS, COLUMNS];

This works and the 2D array is filled with GameObjects properly, but when I try to reference the 2D array in the Update() method of GameBoard (the same class) by doing

// In GameBoard class
 void Update () {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         // Causes NullReferenceException
         Debug.Log(gameBoard[0, 0].name);
     }
 }

I get a NullReferenceException, saying that gameBoard[0, 0] is null. This is odd to me because referencing an element within another method seems to return a valid GameObject, seen in this method:

// In GameBoard class
 public void setGameBoardPosition(int row, int column, GameObject gamePiece)
 {
     gameBoard[row, column] = gamePiece;
     Debug.Log("Setting X: " + row + " Setting Y: " + column + " with GamePiece: " + gameBoard[row, column].name);
     // Prints just fine
 }

Am I initializing the 2D array in the wrong place or in the wrong way?

3 possibilities:

  1. If it happens on invocation of setGameBoardPosition(), this only means that the gameBoard itself was not initialized in your other class. Hence it is null, and does cannot invoke setGameBoardPosition().

For it to work, make sure that this other class has gameBoard both declared and pointed to the propper GameBoard instance (during Awake for example), with GameObject.Find() followed by GetComponent() or by dragging through inspector.

  1. If error happens inside the setGameBoardPosition(), on

gameBoard[row, column] = gamePiece; this means gameBoard within the GameBoard class is not initialized (highly unlikely).

  1. If error happens inside the setGameBoardPosition(), on

    Debug.Log("Setting X: " + row + " Setting Y: " + column + " with GamePiece: " + gameBoard[row, column].name);

this would mean gameBoard array was once again not initialized within your GameBoard class (again, highly unlikely)

I’ve fixed my problem by changing private GameObject[,] gameBoard; to private static GameObject[,] gameBoard;

Now Update() is properly referring to the same array edited by setGameBoardPosition(). Before, it seemed like gameBoard[,] accessed by Update() and by setGameBoardPosition() were two different and separate arrays (in Update() it was null, but in setGameBoardPosition(), it had proper references). Can anyone explain why this worked?