You’re quite right, that some error checking along the way would be helpful. As a rule of thumb, I tend to say that if something can be null, then it should be null-checked along the way.
Let’s conqure the issues you’re having, starting with the one displayed.
It looks like PathFinding’s GridSystem is being created, and it looks like you are calling CreateDebugObjects() in Pathfinding’s Setup method (because the debug objects themselves exist, and the Debug Object is the one we use in Pathfinding). Were either of these things not happening, you would not have a grid on the floor.
It looks, though, like the GridSystemDebugObject itself isn’t getting information about the GridObject.
Let’s start with a look at GridSystem.CreateDebugObjects()
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));
}
}
}
As the grid of debug objects was instantiated, it’s unlikely that gridDebugObject was null, as in that case, it would have returned a null reference error the first time gridDebugObject.SetGridObject was called.
That leads us to PathfindingGridDebugObject.cs:
public class PathfindingGridDebugObject : GridDebugObject
{
[SerializeField] private TextMeshPro gCostText;
[SerializeField] private TextMeshPro hCostText;
[SerializeField] private TextMeshPro fCostText;
private PathNode pathNode;
public override void SetGridObject(object gridObject)
{
base.SetGridObject(gridObject);
pathNode = (PathNode)gridObject;
}
protected override void Update()
{
base.Update();
gCostText.text = pathNode.GetGCost().ToString();
hCostText.text = pathNode.GetHCost().ToString();
fCostText.text = pathNode.GetFCost().ToString();
}
}
It’s possible that a null reference was sent to Setup, we can check this with a simple Debug (ok, 2) in Setup:
public override void SetGridObject(object gridObject)
{
base.SetGridObject(gridObject);
if(gridObject==null)
{
Debug.LogError($"PathfindingDebugObject passed a null GridObject {transform.position}");
return;
}
pathNode = (PathNode)gridObject;
if(pathNode==null)
{
Debug.LogError($"PathfindingDebugObject passed an object that is not a PathNode");
return;
}
Debug.Log($"PathfindingDebugObject set at {pathNode.gridPosition});
}
If you get either of the Debug.LogErrors, then the issue may be in the GridSystem.GetGridObject(gridPosition)) method.
If you get NO debugs, then it is likely that the method is not being called at all in CreateDebugObjects