A* Pathfinding Project

Keep getting no nodes passed exception


#1

Here is my current code. Not sure how to fix with without a while loop of recursive solution, but which case more issues. How do I make it keep trying or why is it unable to find nodes in the first place?

using UnityEngine;
using System.Collections;
using Pathfinding;
using System.Collections.Generic;
public class WanderingDestinationSetter : MonoBehaviour
{
    public float radius = 20;

    IAstarAI ai;

    void Start()
    {
        ai = GetComponent<IAstarAI>();
    }
    private void OnDrawGizmos()
    {
        Gizmos.DrawSphere(this.gameObject.transform.position, radius);
    }
    Vector3 PickRandomPoint()
    {
        var startNode = AstarPath.active.GetNearest(transform.position).node;
        var nodes = PathUtilities.BFS(startNode, 20);
        var singleRandomPoint = PathUtilities.GetPointsOnNodes(nodes, 1)[0];
        var multipleRandomPoints = PathUtilities.GetPointsOnNodes(nodes, 100);

        return singleRandomPoint;
        //var point = Random.insideUnitSphere * radius;

        //point.y = 0;
        //point += ai.position;
        //return point;
    }

    void Update()
    {
        // Update the destination of the AI if
        // the AI is not already calculating a path and
        // the ai has reached the end of the path or it has no path at all
        if (!ai.pathPending && (ai.reachedEndOfPath || !ai.hasPath))
        {
            ai.destination = PickRandomPoint();
            ai.SearchPath();
        }
    }
}

#2

Hi

I’m guessing that your start node is not walkable, so your BFS will return an empty list.
In the GetNearest call, try instead

GetNearest(transform.position, NNConstraint.Default);

that will cause it to find the closest walkable node instead of just the closest node.
I will update the tutorial with this change as well.