Null reference Error after first update

Hello everyone,

I have a problem with Null Reference Exception Error. The previous two topics here sadly didn’t help me.

The error happens after the first update in GridDebugObject class. The first time a grid obejct is set for GridDebugObject, the labels are set correctly, however in every second update, both the TextMeshPro & gridObject are null. Could I have an empty gridDebugObject somewhere??

GridDebugObject class:

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

public class GridDebugObject : MonoBehaviour
{
    [SerializeField] private TextMeshPro textField;

    private GridObject gridObject;

    private void Update()
    {
        if (gridObject == null)
        {
            Debug.Log("GridObejct is null;");
        } else
        {
            Debug.Log("GridObject GOOD");
        }

        if (textField == null)
        {
            Debug.Log("Text is null;");
        }
        else
        {
            Debug.Log("Text GOOD");
        }

        UpdateGridObjectLabel();
    }

    public void SetGridObject(GridObject gridObject)
    {
        this.gridObject = gridObject;
    }

    private void UpdateGridObjectLabel()
    {
        textField.text = gridObject.ToString();
    }
}


GridSystem class

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

public class GridSystem
{
    // Grid dimensions / x & z axis
    private int width;
    private int height;
    private float cellSize;
    private GridObject[,] gridObjectArray;

    // GridSystem Constructor
    public GridSystem(int width, int height, float cellSize)
    {
        this.width = width;
        this.height = height;
        this.cellSize = cellSize;

        gridObjectArray = new GridObject[width,height];
        // Cycling through our grid
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                // Creating grid objects for each grid tile
                GridPosition gridPosition = new GridPosition(x, z);
                gridObjectArray[x, z] = new GridObject(gridPosition, this);
            }
        }
    }

    // Get world position from grid position
    public Vector3 GetWorldPosition(GridPosition gridPosition)
    {
        return new Vector3(gridPosition.x, 0, gridPosition.z) * cellSize;
    }

    // Get grid position from world position
    public GridPosition GetGridPosition(Vector3 position)
    {
        return new GridPosition(
            Mathf.RoundToInt(position.x / cellSize),
            Mathf.RoundToInt(position.z / cellSize)
            );
    }

    // Gets the grid obejct from grid array
    public GridObject GetGridObject(GridPosition gridPosition)
    {
        return gridObjectArray[gridPosition.x, gridPosition.z]; 
    }

    // Creates grid visualization coordinates
    public void CreateDebugObjects(Transform debugPrefab)
    {
        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                // Position in grid 
                GridPosition position = new GridPosition(x, z);
                // Instantiate the text (debug object)
                Transform debugTransform = GameObject.Instantiate(debugPrefab, GetWorldPosition(position), Quaternion.identity);
                // Get the Grid Debug Object component - to set the GridObject
                GridDebugObject gridDebugObject = debugTransform.GetComponent<GridDebugObject>();
                // Assign the gridObject to a DebugObject
                gridDebugObject.SetGridObject(GetGridObject(position));
            }
        }
    }
}

Any help would be appreciated, thanks

After a little update I made to the Update method in GridDebugObject, it now seems, that the null object is the actual GameObject in hierarchy, where as the instance doesn’t have a parent.

    private void Update()
    {
        if (gridObject == null)
        {
            Debug.Log("GridObejct is null; " + transform.parent);
        } else
        {
            Debug.Log("GridObject GOOD" + transform.parent);
        }

        if (textField == null)
        {
            Debug.Log("Text is null;" + transform.parent);
        }
        else
        {
            Debug.Log("Text GOOD" + transform.parent);
        }

        UpdateGridObjectLabel();
    }

However, the createDebugObjects method only creates one (checked throu visual studio debugging)

I found the issue, after further inspecting the transform.parent in Visual Studio.

Even though Unity didn’t print the parent properly I found out that it was RectTransform, so I inspected my DebugPrefab and found out, the script was also attached to the text itself.

Sorry for posting, maybe it will help someone whos almost as dumb as me :smiley:

Not dumb, this is something that happens to all of us… other common issues include a GridDebugObject being left in the scene after we create the prefab.

1 Like

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

Privacy & Terms