30 minutes with nearly no error checking?

My level grid is now producing only values of 0,0 and the testing script is returning the same problem someone else had here:

This error seems to arise from a possible error in the backend after the shift to generics in the levelgrid, which is going to happen when you move this quickly. I’m not very invested in a solution since I can just pull the state of the project at the time from the gitlab and expend no effort on this, but I’m not sure how anyone could really be expected to not produce profound errors with so few mechanisms to catch them.

An image of one issue:
Capture

The current state of the scripts is the one on the GitLab, because I’ve just copy-pasted over my own code. It’s actually not fixed.

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

Tried this out, I ended up getting a permissions issue from calling pathNode.gridPosition since I don’t think that’s set public as is, but I was able to spawn some dummy debug code from the last line anyway. I ended up resolving this issue by setting using system on the testing script though. No idea why that did it. Thanks for the help!

Good point, there actually should be a method in PathNode that exposes the gridPosition:

    public GridPosition GetGridPosition()
    {
        return gridPosition;
    }

Privacy & Terms