Issues with accessing a 2D array from another script

I have a script that generates a grid of tiles. Each tile is a prefab that contains information that I need to grab later.

script A:

public static Tile[,] _tiles;

I populate the 2D array with my instantiated prefab within a nested For loop.

_tiles[r_i, c_i] = Instantiate(_tilePrefab, new Vector3(c_i, r_i, 0), Quaternion.identity);

In script B which is attached to a button I want to be able to copy the _tile array from script A. So I made a dummy variable and tried to assign the array values to it. rowNum and columnNum are public static variables from script A.

script B:

        Public Tile[,] copyA;
        Tile[,] copyA = new Tile[A.rowNum,A.columnNum];
        for (int x = 0; x < A.rowNum; x++)
        {
            for (int y = 0; y < A.columnNum; y++)
            {
                copyA[x, y] = A._tiles[x, y];
            }
        }

In script A I can verify that the prefab is getting placed in the array _tiles when I set a breakpoint in script A. The error I get after pushing the button for script B is “Object reference not set to an instance of an object”. Somehow my array _tiles is null in script B even though in script A there are Tile objects in it. What am I missing?

As a test, in script A I assigned the prefabs into a public static dictionary. In script B I did var test = A._tiles. I’m able to see my prefabs now. However, for my purposes a dictionary will not work for what I need to do later.

There’s nothing special about this NullReference error. Most likely you simply are not executing the line that allocates the Tile[,] array before you attempt to use it.

Generally if you have something that holds a bunch of data, you want to make an API for other things to use it. It can be static, that’s fine, but make a clear pattern of explicit functions for the user to call:

  • initialize
  • use it:
    ----> give me a tile
    ----> set a tile
  • un-initialize (if necessary) at end of game

EVERYTHING besides the above functions would be private.

This lets you in the future completely change out how you store the data and nobody else in your program would ever know, as long as the above functions continue to work.

Meanwhile, here’s the usual blurb:

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7

Identify what is null: _tiles is null in script B, but not in script A
Identify why it is null: no idea since _tiles is public static and has objects in it when I debug script A

Instead of copying an array from A to B I copied a dictionary. I was able to see the objects in my dictionary having the same public declaration. The only thing that will be accessing this 2D array is script B.

No need to change how I store the data in the future. This “game” is a program that solves a puzzle and is for myself. Each instantiated tile came from a prefab tile that has a script “TileProps.cs” attached to it to store it’s row and column position in the grid as well as any other object that is on that grid tile. The goal after I write up the algorithm code is to path find for each object on a tile using the TileProps class to access row and column position as well as object color info . In script B I did write a routine that stores the contents of each tile in a 2D array. In this case I store an integer value that represents the rgb color of the object placed on a tile. I am able to copy this array in script B and can see it’s contents with no issue.

Attached an example image of what I’m doing.