0

I've written a script for an enemy that moves along tiles on a grid, which is a 3D array with the first dimension being horizontal position, second vertical position and 3rd properties of the space. I have not referenced any objects, yet when I run the script it causes an "Object reference not set to an instance of an object" error.

public bool my_turn;
public bool turn_setup;
public List<int> my_grid_pos;
public List<List<List<int>>> grid;
void Update () {
    if(my_turn)
    {
         if (turn_setup)
         {
             grid[my_grid_pos[1]][my_grid_pos[0]][2] = 1;
         }
    }
}
Le Pain
  • 75
  • 9

1 Answers1

1

"I have not referenced any objects"

Yes, you are. On this line you're trying to reference 4 objects:

grid[my_grid_pos[1]][my_grid_pos[0]][2]
  • my_grid_pos[1]
  • my_grid_pos[0]
  • grid[x, y]
  • grid[x, y][2]

One (or more) of these list queries results is resulting in a null value, for some reason or another, that we cannot deduct from the code you've posted. You then try to access one of its list's objects:

null[x]

This results in the error you're getting.


Here's an answer explaining what NRE is

Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32