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