- Home /
HELP!!! This NullReferenceException won't go away! :(
Title more or less says it all. I am working on a custom board generator for a board game. Long story short I keep getting the error
NullReferenceException: Object reference not set to an instance of an object GridGenerator.GenerateGrid (UnityEngine.Texture2D t) (at Assets/Scripts/GridGenerator.cs:37) GridGenerator.Start () (at Assets/Scripts/GridGenerator.cs:24)
To be clear I have all fields filled. In fact, I checked the validity of the texture asset throughout the entire script and it always is present. However, no matter what I do, it keeps throwing this error for no reason.
Here's some of code. Pls help(...it refuses to leave. ε(´סּ︵סּ`)з ) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridGenerator : MonoBehaviour
//creates the actual grid
{
public Texture2D levelMap;
//a texture from which to create the board
public GameObject nodeGO;
//empty gameobject to tag later
public List<GridNode> nodes;
//list of working nodes AKA the actual board
void Awake()
{
Debug.Log(levelMap);
//the texture exists
}
void Start() {
Debug.Log(levelMap);
//the texture exists
GenerateGrid(levelMap);
}
public void GenerateGrid(Texture2D t){
Debug.Log(t);
//the texture exists
for(int x = 0; x <= t.width; x++){
for(int y = 0; y <= t.height; y++){
//iterate through the grid
Instantiate(nodeGO, new Vector3((float)x,0.5f,(float)y),Quaternion.identity);
//create the node game object for visuals
nodes.Add(new GridNode(new gridPos(x,y),
FindNeighbors(x,y,t),//<--the troublemaker?
false));
}
}
}
public gridPos[] FindNeighbors(int x, int y,Texture2D t){
Debug.Log(t);
//the texture exists
gridPos[] n = new gridPos[8];
//making sure the array doesn't exceed the maximum size for the GridNode
int i = 0;
//array element counter
for (int a = x-1; a < x+1; a++){
for (int b = x-1; b < x+1; b++){
if(((!(a<0))||(!(a==x))||(!(a > t.height)))&&((!(b < 0))||(!(b==x))||!(b > t.width))){
//long story short check if there is a neighbor. if there is one add it to the array
n[i]=new gridPos(a,b);
Debug.Log(n[i]);
//the console thinks of itself to important now
i++;
//increment array element counter
}
}
}
Debug.Log(n);
//the console is reporting a spammer
return n;
//return the array of neighbors
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//an actual grid cell
public class GridNode{
public gridPos[] neighbors {get; private set;} = new gridPos[8];
public readonly gridPos Coord;
//it's grid position
public bool isOccupied;
//occupied or not?
//constructor
public GridNode(gridPos pos, gridPos[] n, bool o){
this.Coord = pos;
this.neighbors = n;
this.isOccupied = o;
}
}
//fancier than necessary class to pass around grid positions
public class gridPos {
public int x,y;
public gridPos(int x, int y){
this.x = x;
this.y = y;
}
}
If you can try to format the code it would make finding the error easier. I'm trying to figure out which part is on line 37. So far I see a nodeGO that would cause the error if it's not assigned in the editor.
Answer by jkpenner · Jan 12, 2020 at 04:36 AM
Looks like you are never assigning a new list to the 'nodes' list.
public List<GridNode> nodes;
Should be:
public List<GridNode> nodes = new List<GridNode>();
Your answer
Follow this Question
Related Questions
Scripting Help with My Projectile System 1 Answer
Null reference when accessing GameObject in the Array(C#) 1 Answer
NullReference when accessing GameObject in array (C#) 1 Answer
MergeSort function acting strange 0 Answers
Unable to use global Dictionary in function. "NullReferenceException" 1 Answer