Null reference exception for gridManager in Edit mode and Play mode

Hi there, I’m absolutely loving this section so far and have learned a lot. I’ve run into a problem in this lecture that I can’t seem to get past on my own.

When I revised the coordinate color method (I’ve named mine UpdateCoordinateStyle) to follow this lecture, I’m getting a null reference exception - but not the one that Gary calls out in the video.

My problem is with the value of gridManager itself. Here is what my method looks like:

    private void UpdateCoordinateStyle()
    {
        if (gridManager = null) return;
        
        if (gridManager.GetNode(coordinates) == null) return;
        
        Node node = gridManager.GetNode(coordinates);
        
        if (node == null) return;
        
        if(!node.isWalkable)
        {
            label.color = blockedColor;
        }
        else if (!node.isPath)
        {
            label.color = pathColor;
        }
        else if (node.isExplored)
        {
            label.color = exploredColor;
        }
        else
        {
            label.color = defaultColor;
        }
        
    }

I am receiving an editor error at each frame for every instance of the CoordinateLabeler script. I’ve tried disabling this method in editor mode, which removes the editor error. However, I still see the error in every frame in Game mode.

I thought that I had tracked the problem down to the Awake() method not firing in Editor mode - I’m using the following code in my Awake():

    void Update()
    {
        if(!Application.isPlaying) //runs the following block on every frame when the application is in Edit mode (NOT play mode)
        {
            DisplayCoordinates();
            UpdateObjectName();
            label.enabled = true;
        }
        if(Application.isPlaying)
        {
            UpdateCoordinateStyle();
        }

        ToggleCoordinateOverlay();
    }

Note that I’ve mode UpdateCoordinateStyle() into its own conditional. As stated above, however, the errors are still present in Play mode.

To top it all off, I’ve created a separate script (for testing purposes only), which has no problem finding the gridManager or running the gridManager.GetNode method. So there seems to be something else happening in the CoordinateLabeler script. Here is that script:

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

public class FindGridManager : MonoBehaviour
{
    GridManager gridManager;

    void Start()
    {
        gridManager = FindObjectOfType<GridManager>();
        Vector2Int coordinates = new Vector2Int(1, 2);
        Debug.Log("gridManager loaded");

        if (gridManager.GetNode(coordinates) == null)
        {
            Debug.Log("The node at " + coordinates + " does not exist.");
        }
        else
        {
            Debug.Log("Success! Node at " + coordinates + " exists.");
            Node node = gridManager.GetNode(coordinates);
            Debug.Log("isWalkable: " + node.isWalkable);
        }
    }
}

Any help would be greatly appreciated as I have been trying to track down this bug for a full day now and it’s quite frustrating, as everything is written correctly per the lecture and Unity documentation (as far as I can tell).

Thanks,
Matt

1 Like

I figured it out! And of course, it was a “minor” syntax error that broke everything. :slight_smile:

In the CoordinateLabeler → UpdateCoordinateStyle() method, I was assigning gridManager the value of null (one equals sign), rather than comparing it against null (2 equals signs). Embarrassing but glad that I found it after all this struggle.

Best,
Matt

1 Like

Good job, @mattgreggs, and welcome to our community! :slight_smile:

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

Privacy & Terms