NullRefrenceException

I’m getting a NullRefrence exception when I try to do the last part of this tutorial.

As far as I can tell, there’s nothing different about this line and I’m doing what was done in the tutorial. Any help would be appreciated!

I would first make sure you attached what you needed to attach in the inspector in the script category

Either textMeshPro or gridObject is null. Make sure you have something specified in the inspector for textMeshPro and make sure that the gridObject you are passing to SetGridObject is not null. You can add some Debug.Log() statements to check

private void Update()
{
    Debug.Log($"textMeshPro is null: {textMeshPro == null}");
    Debug.Log($"gridObject is null: {gridObject == null}");

    textMeshPro.text = gridObject.ToString();
}

One of those will be ‘true’ and lead you in the right direction

1 Like

So , it says that the gridObject is null, but I assigned it in the start function like it was shown in the video.

I tried to serializefield it and assign it manually but it didn’t show up in the inspector.

It’s being set in the GridSystem class when we do the CreateDebugObjects()


It’s supposed to look like this, right? This is what I saw in the video.

Yeah, that’s correct. Make sure

  • The GetGridObject() method actually returns something other than null
  • There isn’t a GridDebugObject somewhere in your hierarchy

The GetGridObject method is returning null, but since its only one line of code I’m not sure how to fix it.

Can you show the code?

So, this is returning an object from the array of objects. Did you set any objects in the array? It should happen in the constructor of GridSystem

Yes, I have

So I forgot we could look at the scripts from this project, so I found out where I went wrong
I forgot the “for” loop in the GridSystem method. Sorry about that.

1 Like

No problem. At least your problem is solved now

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

Privacy & Terms