NullReferenceException: Object reference not set to an instance of an object


It definitely gets to this line but not sure why gridObject wouldn’t exist at this point in the code?
I have the prefab of GridDebugObject attached to the SerializeField for LevelGrid.
have I messed up the LevelGrid cide somewhere?
image

Is it possible your textMeshPro hasn’t been set in the inspector?

I have set it to the TextMeshPro that is in the Prefab for GridDebugObject. That’s the correct one according to the previous lecure (3a_GS_UTS)

Let’s add a quick debug.Log to ensure things are hooked up correctly…

public void SetGridObject(GridObject gridObject)
{
    this.gridObject = gridObject;
    if(gridObject==null) Debug.Log($"Attempting to setup a GridDebugObject with a null GridObject");
    if(textMeshPro==null) Debug.Log($"TextmeshPro is not setup in the inspector");
}

Ok so I figured it out.
Added above Debugs. Nothing showed up in the log.
Added more debugs to GridSystem where SetGridObject is called. Nothing showed up in the log.
Added more debugs to LevelGrid where CreateDebugObjects is called on awake. Nothing showed up in the log.

Realised that I had named the function awake instead of Awake. Kind of silly that it doesn’t autocorrect that. You mean to tell me that enough people are creating functions named ‘private void awake()’ that are something different to the Awake function that they can’t afford to capitalise the ‘a’ by default?

Anyway, thanks for the help everyone appreciate it!

Unfortunately, this is one of those things that the code editor and Unity don’t check for.
The way the callbacks (Awake(), Start(), etc) work, if they are not present within the script, Unity simply assumes they are not there and carries on. If it’s misspelled or not in PascalCase, then it’s just assumed that this is what you intended.

Some code editors (like JetBrains Rider) will mark your Callback functions with a notation “Unity Callback”, and will gray out the names on a method that is never called from anywhere (like awake()). Of course, Rider isn’t free.

Hold Up I am getting the error again. I am now watching 6_GS_UTS and after some debuging I get this:


It looks like the error is happening on the instantiated GridDebugObject (the one that exists in the scene before I play the game)."All of the preceding “I get here” show that the ones in the 10x10 grid work fine.
But as you can see the selected GridDebugObject has an instance of TextMeshpro attached to it (otherwise it wouldn’t have the 1,2).

Yeah, that specific instance is not part of the grid, so it never gets a SetGridObject() call. But the Update() still runs and then there is no gridObject.

Edit Looking at the error, it seems it is in fact getting a SetGridObject() call, but there is no gridObject being passed to it

It also seems to get that call after the grid has completed the CreateDebugObjects(). So, somewhere, something is calling SetGridObject() on the instance. Unless your Debug.Log that printed just before the error is in the Update() and not in the SetGridObject() method. Then it is simply because the instance is not part of the grid and never gets a SetGridObject() call

The correct solution is to remove the instance from the scene

Yep your right I deleted the instance of GridDebugObject in the scene and it works.

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

Privacy & Terms