Null reference error

i am getting a null reference error saying the instance is not set to a reference of an object. ive been trying to figure out what im doing wrong but i cant find the problem. my code is the same as the instructor its saying line 34 is the problem which is the line with the if statement for neighborcoords

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

public class Pathfinder : MonoBehaviour
{
[SerializeField] Node currentSearchNode;
Vector2Int directions = {Vector2Int.right, Vector2Int.left, Vector2Int.up, Vector2Int.down};
GridManager gridManager;
Dictionary<Vector2Int, Node> grid;

void Awake()
{
    gridManager = GetComponent<GridManager>();
    if (gridManager != null )
    {
        grid = gridManager.Grid;
    }
}
void Start()
{
    ExploreNeighbors();
}

void ExploreNeighbors()
{
    List<Node> neighbors = new List<Node>();

    foreach (Vector2Int directions in directions)
    {
        Vector2Int neighborCoords = currentSearchNode.coordinates + directions;


        if (grid.ContainsKey(neighborCoords))       < this is the line unity is saying is the problem
        {
            neighbors.Add(grid[neighborCoords]);
        }


        //Remove after testing
        grid[neighborCoords].isExplored = true;
        grid[currentSearchNode.coordinates].isPath = true;
    }


}

}

Based on the line you highlighted, it’s possible that grid is null. You have no way of knowing if the GridManager was even found on the game object and if it wasn’t, grid would be null. Make sure there is a GridManager component on the same object as the Pathfinder component. The safest here is to actually remove the code in Awake() that checks if GridManager null

void Awake()
{
    gridManager = GetComponent<GridManager>();
    grid = gridManager.Grid;
}

Your component depends on a GridManager so the check is actually bad. You want the code to fail at runtime so that you as developer can fix it. Also, I highly recommend you add [RequireComponent(typeof(GridManager)] at the top of the class because the pathfinding does, in fact, require a GridManager and this will ensure that you have one.


Edit
I had a look at the course code because I felt there was too much wrong here to be in the course. Your code is not the same as Gary’s code. Gary uses FindObjectOfType<GridManager>() to get the GridManager. You use GetComponent<GridManager>(). There is a big difference. Gary will find a GridManager anywhere in the scene. You will only find a GridManager if it’s part of this game object. Perhaps you need to go through the lecture again and make 100% sure you follow what Gary is doing. Note that Gary still has the issue that his Pathfinder depends on GridManager and if there isn’t one in the scene he’ll get the NRE, but the [RequireComponent()] won’t help with this.

i went thru my code multiple times and never saw the get component line i dont even remember putting that in. i have also inserted the gridmanager script into the same gameobject as my pathfinder but im still getting the same problem. im doing my best to follow along verbatim with him but my head is spinning with all the concepts that i dont know or understand

Can you show the GridManager script and a screenshot of the game object?

Privacy & Terms