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);