A tale of woe (AKA debugging)

I just wanted to share my recent debugging challenge after the More Debugging Tools lesson. Hopefully someone will draw inspiration from this to never give up or at least just to know how a tiny error can manifest in a confusing way.

I found that I was getting a similar error to what the instructor had seen. Namely "Object reference not set to the instance of an object). The error was referencing the same line as the code calling for a new node Node node = gridManager.GetNode(coordinates); In the lesson, the problem was resolved by adding if (node == null) { return; } I added this to my CoordinateLabeler script, but it kept occurring. So on to debugging…

I combed through the instructor’s project changes in GitLab - all seemed correct

I then thought maybe it was something else in my scene. I created a test scene with one tile. The error was still occurring.

However, I then realized that I didn’t even have a GridManager object in my scene.

I added and removed a GridManager object. Still the same result. I was really starting to question my sanity.

I started looking at the GridManager and Node scripts. Nothing seemed to be out of place there.

And then I saw it. I’ll put it below. See if you can spot what I did wrong.

void SetLabelColor()
    {
        if (gridManager = 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; }
        

    }

If you couldn’t find it, I had set the gridManager variable to null in my IF statement instead of using a == as I should have. :tired_face: This means the GridManager object that I set in gridManager at Awake got removed. Therefore, if I was trying to reference that object to get a node, there was no GridManager to pull it from. Once I updated the statement with an extra equal sign, everything worked fine.

2 Likes

I had done the exact same thing. Walked away and had dinner and relaxed for 30 minutes came back and saw it right away. Glad you found the coding error!

2 Likes

I did the same thing you did! How though? I don’t know. I swear I was paying attention and following along.

if(gridManager == null) {return;}

Really threw me off. Thanks though for your tale! Saved me from searching.

2 Likes

Privacy & Terms