James Stephen Brown
James Stephen Brown

Reputation: 299

Getting Argument out of range during pathfinding Coroutine

I'm new to using coroutines and am getting and out of range array error when running this function. I'm hoping it is a simple problem contained within this function, perhaps I am yielding at the wrong point, or need to use path.count -1.

public void UpdatePath() {
    the.pathFinder.FindPath (my);

    Vector3[] pathVectors = new Vector3[my.path.Count + 1];

    pathVectors [0] = (Vector3)my.transform.position;

    if (my.path.Count > 0) {
        for (int i = 0; i < my.path.Count; i++)
            pathVectors [i + 1] = my.path [i].transform.position;
    }

    my.pathLine.SetVertexCount (my.path.Count + 1);
    my.pathLine.SetPositions(pathVectors);

    StopCoroutine("FollowPath");
    StartCoroutine("FollowPath");
}

IEnumerator FollowPath() {

    Vector3 currentWayPoint = my.path [0].transform.position;

    while (true) {

        Vector2 heading = the.Heading (my.transform.position, currentWayPoint);

        if (heading.sqrMagnitude < 0.4) {
            targetIndex++;
            if (targetIndex >= my.path.Count) {
                print ("On yield break - TargetIndex is " + targetIndex + " and my.path.Count is " + my.path.Count);
                yield break;
            }
            print ("On assigning currentWayPoint - TargetIndex is " + targetIndex + " and my.path.Count is " + my.path.Count);
            currentWayPoint = my.path [targetIndex].transform.position;
        }

        my.pathGoal = ((heading).sqrMagnitude > 1f) ? heading.normalized * my.speed : heading * my.speed;
        yield return null;
    }
}

I have put in a print log to check targetIndex against my.path.Count. To my understanding, in the second print log, the targetIndex should not be able to be equal to or greater than the my.path.Count and yet in the log, I'm getting "On assigning currentWayPoint - TargetIndex is 1 and my.path.Count is 1". I realise this is why I'm getting the out of range exception, but I don't understand how the code is reachable if the TargetIndex is equal to the my.path.Count. Is it possible for something else to happen between the yield break; line and the print log? Do coroutines run simultaneously with other code that could conflict?.

Upvotes: 0

Views: 296

Answers (1)

Gasper
Gasper

Reputation: 3030

This has nothing to do with coroutines.

You're trying to access member with index higher than the number of array elements:

for (int i = 0; i < my.path.Count; i++)
    pathVectors [i + 1] = my.path [i].transform.position; // will fail on i = path.Count

Moreover, compiler tells you at what point in the code this exception happens, which should pretty easily lead you to a conclusion you have already reached:

need to use path.count -1

It's always a good idea to try what's on your mind in code to see if that works, before asking questions on SO.

Also, the if statement around this code is meaningless.

Upvotes: 1

Related Questions