Getting an Error

I got an error after following the lecture: NullReferenceException: Object reference not set to an instance of an object
Pathfinder.Awake () (at Assets/Pathfinding/Pathfinder.cs:24)

my Awake method:
void Awake()

{

    {

        grid = gridManager.Grid;

    }

    startNode = new Node(startCoordinates, true);

    destinationNode = new Node(destinateCoordinates, true);

}
1 Like

Where do you assign gridManager? That’s probably where the error comes from.

Because I had a different problem before, I just copied the gitHub Commit from this lecture:

public class GridManager : MonoBehaviour

{

[SerializeField] Vector2Int gridSize;

Dictionary<Vector2Int, Node> grid = new Dictionary<Vector2Int, Node>();

public Dictionary<Vector2Int, Node> Grid { get { return grid;} }

void Awake()

{

    CreateGrid();

}

public Node GetNode(Vector2Int coordinates)

{

    if(grid.ContainsKey(coordinates))

    {

        return grid[coordinates];

    }

   

    return null;

}

void CreateGrid()

{

    for(int x = 0; x < gridSize.x; x++)

    {

        for(int y = 0; y < gridSize.y; y++)

        {

            Vector2Int coordinates = new Vector2Int (x,y);

            grid.Add(coordinates, new Node(coordinates, true));

        }

    }

}

}

Many Thanks once again!

1 Like

Yes, There’s a grid manager component, but you are referencing a variable that has not been assigned to it

1 Like

how do i fix it? how do i assign it?

Many Thanks!

From the course repo:

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

        startNode = new Node(startCoordinates, true);
        destinationNode = new Node(destinateCoordinates, true);
    }
1 Like

thank you so much!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms