Check for error before using AIPath

Hey,

I like to check if the path is reachable before using sending the unit on its way. Currently Im using the “AIPath” but I cant find a way to catch if the path is not reachable.

However, I can catch errors with the “Seeker”. I made a temporary solution like this:

private AIPath pathfinding;
private Seeker seeker;

private IEnumerator SearchPath(Vector3 taskPosition)
{
    var path = seeker.StartPath(taskPosition, transform.position);
    yield return StartCoroutine(path.WaitForPath());

    if (!path.error)
    {
        pathfinding.canSearch = true;
        pathfinding.isStopped = false;

        // Set destination
        pathfinding.destination = taskPosition;
        isMoving = true;
    }
    else
    {
        onTaskDone(false);
    }
}

However, the path gets (logicly) calculated two times now.

I tried somting like

pathfinding.SetPath(path);

However, this gave me an error:

My question, is there a clean way to first check if there is a path (no error) before sending the AIPath, without calculating the path twice.

An easier way to check if a path is possible is using the IsPossible function: https://arongranberg.com/astar/docs/pathutilities.html#IsPathPossible

Once you know the path is possible you can just do the normal StartPath

1 Like

Thank you so much.

I did get a NullReference error when using the example. But when I deleted “NNConstraint.Default” it works just fine.

Can I just delete this, or will this couse problems later on?