[SOLVED]Argument out of range exception on my custom path-finding script.

I decided to attempt to make my own path-finding, rather than use the built-in system. (I just feel more at home with code that I write myself, I usually have problems understanding other peoples work)

I really thought long and hard about how I was going to approach it, so i decided to go with a “Waypoint” approach. The way my script works (So far) is that it creates 2 lists. The first list it creates stores references to the vector3 of all the waypoints within the area, and the second list stores a sequence of vector3’s that always reach the target object. (Its not always the shortest path yet I haven’t gotten that far).

The problem is, I get an error at runtime, which says:

I am calling the function from the AI player inside of fixed update like this

        if (targetObject != null) {
            target = pathfinder.BeginPathing (transform, targetObject);
        }

And the path-finding code:

    [SerializeField] float radius;
    private List<Vector3> waypoints = new List<Vector3>();
    private List<Vector3> path = new List<Vector3> ();
    private int pathLayer;
    private int blockLayer;
    private Transform source;
    private Transform target;
    private Transform nextWaypoint;

    // Use this for initialization
    void Start () {
        pathLayer = 1 << 9;//LayerMask.NameToLayer("Pathfinding");
        blockLayer = 1 << 10;
    }

    public Vector3 BeginPathing(Transform sourceObject, Transform targetObject) {
        source = sourceObject;
        target = targetObject;
        if (target != null) {
            Collider[] hit = Physics.OverlapSphere (sourceObject.position, radius, pathLayer);
            if (hit != null) {
                foreach (Collider waypoint in hit) {
                    //Generate list of waypoints.
                    waypoints.Add (waypoint.transform.position);
                }
                if(path == null) {
                    FindPath();
                    return path[0];
                } else {
                    if(sourceObject.position == path[0]) {
                        path.RemoveAt (0);
                    }
                    return path[0];
                }
            } else
                return Vector3.zero;
        } else {
            return Vector3.zero;
        }

    }


    void FindPath() {
        RaycastHit hit;
        bool success = false;
        path.Add (source.position);
        for (int i = 0; i < path.Count; i++) {
            if (!Physics.Linecast (path[i], target.position, out hit, blockLayer)) {
                //If there is nothing between me, and the target object.
                path.Add (target.position);
                success = true;
                break;
            }
            for(int j = 0; j < waypoints.Count; j++) {
                if (!Physics.Linecast (path[i], waypoints [j], out hit, blockLayer)) {
                    //If there is nothing between the current waypoint, and the target waypoint.
                    path.Add (waypoints[j]);
                }
            }
        }
    }
}

After doing a few tests I know that my code isn’t really all that efficient. My plan was to make it so that the “FindPath” function would only be done once every 2 seconds or so, and I’m sure there are other places for improvement. I’m not exactly a great programmer, I’ve been stuck on this for hours and I can’t figure it out. Any help would be appreciated.

I solved it, this problem was caused by checking to see if my list was “null”, rather than checking if the list contained any items. Since the list was declared at the top of my script, it was never null, so my function gave me an error.

1 Like