Null Reference Exception Error

Solved my issue

Hello, I am getting a null ref exception error and I have spent a few hours no trying to chase it down with no luck.

The error information from the console points to the following line of code with the full text of the error being:
NullReferenceException: Object reference not set to an instance of an object
GridDebugObject.Update () (at Assets/Scripts/Grid System/GridDebugObject.cs:15)

 textMeshPro.text = gridObject.ToString();

I cannot find any differences in code from what I have to what the instructor has.
The full text of the script containing the error is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GridDebugObject : MonoBehaviour
{
    [SerializeField] private TextMeshPro textMeshPro;
    private GridObject gridObject;
    public void SetGridObject(GridObject gridObject)
    {
        this.gridObject = gridObject;
    }
    private void Update()
    {
        textMeshPro.text = gridObject.ToString();
    }
}

The ToString() method being called is as below:

public override string ToString()
    {
        return gridPosition.ToString();
    }

The code I have from the GridSystem.cs file that was added is as below:

public void CreateDebugObjects(Transform debugPrefab)
    {
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                GridPosition gridPosition = new GridPosition(x, z);

                Transform debugTransform = GameObject.Instantiate(debugPrefab, GetWorldPosition(gridPosition), Quaternion.identity);
                GridDebugObject gridDebugObject = debugTransform.GetComponent<GridDebugObject>();
                gridDebugObject.SetGridObject(GetGridObject(gridPosition));
            }
        }
    }
    public GridObject GetGridObject(GridPosition gridPosition)
    {
        return gridObjects[gridPosition.x, gridPosition.z];
    }

Last ditch effort to copy over the instructor files and put them into my project seems to work, but ONLY IF the TextMeshPro variable is named textMeshPro…Changing the variable to anything else breaks the code… Found the error to this as changing the variable name drops the reference in Unity

I am at a complete loss on this one…

Found the root of the problem with some more digging in the GridSystem Struct the GridObject was created but never set into the Array
I had

new GridObject(this, gridPosition);

Instead of

gridObjects[x, z] = new GridObject(this, gridPosition);
1 Like

Based on your edits looks like you already found the issue. In that line the only two possible explanations is either textMeshPro is null or it’s gridObject
I did a video on my process for solving NullReferenceExceptions Code Monkey - How to Fix NullReferenceException in C#!

In that case I would add a Debug.Log(textMeshPro); and Debug.Log(gridObject); before that line to find out which one was null.
Then upon confirming that it was gridObject I would go to the CreateDebugObjects() function and before SetGridObject do a Debug.Log(GetGridObject(gridPosition)); which would lead you to find out that the array element was null which would lead you to see where the array elements should have been assigned.

2 Likes

Thanks for the reply and the video link. I’ve struggled at times to chase down a null ref error, sometimes it’s pretty easy others like this one the answer eludes me

It didn’t occur to me that gridObject was/could be the offender.

I am learning a lot in this tutorial and many thanks for putting it together!

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

Privacy & Terms