- Home /
 
               Question by 
               Nsingh13 · May 18, 2015 at 12:24 AM · 
                androiderroraipathfindingargumentoutofrangeexception  
              
 
              Argument out of Range Exception Error - Parameter Name: Index
Hey guys, so i keep on getting this error for my enemy AI script (which basically uses A* pathfinding to move towards a target)
My error is on this line:
Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
This is the whole script:
 using UnityEngine;
 using Pathfinding;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody2D))]
 [RequireComponent(typeof(Seeker))]
 
 public class EnemyAI : MonoBehaviour
 {
 
         //what to chase
         Transform target;
 
         //restart path
         public bool restartpath = false;
 
         //How many times each second we will update our path
         public float updateRate = 2f;
 
         private Seeker seeker;
         private Rigidbody2D rb;
 
         //the calculated path
         public Path path;
 
         //AI's speed/second
         public float speed = 300f;
 
         public ForceMode2D fmode;
 
         //Waypoint we are currently moving towards
         private int currentWaypoint = 0;
 
         [HideInInspector]
         public static bool
                 pathisEnded = false;
 
         //Max distance from AI to a waypoint for it to continue to the next waypoint
         public float nextWaypointDistance = 3;
 
         void Start ()
         {
                 seeker = GetComponent<Seeker> ();
                 rb = GetComponent<Rigidbody2D> ();
                 target = GameObject.Find ("Roti").GetComponent<Transform> ();
 
                 if (target == null) {
                         //search for target
                 }
 
                 seeker.StartPath (transform.position, target.position, OnPathComplete);
     
                 StartCoroutine (UpdatePath ());
     
         }
 
         IEnumerator UpdatePath ()
         {
                 if (target == null) {
                         //search for target
 
                         return false;
                 }
 
                 seeker.StartPath (transform.position, target.position, OnPathComplete);
 
                 yield return new WaitForSeconds (1f / updateRate);
 
                 StartCoroutine (UpdatePath ());
         }
 
         public void OnPathComplete (Path p)
         {
 
                 if (!p.error) {
                         path = p;
                         currentWaypoint = 0;
                 }
         }
 
 
         void FixedUpdate ()
         {
                 if (target == null) {
                         //search for target
             
                         return;
                 }
 
                 //Optional: Always look at player?
                 Vector3 diff = target.position - transform.position;
                 diff.Normalize ();
 
                 float rot_z = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
                 transform.rotation = Quaternion.Euler (0f, 0f, rot_z - 90);
         
 
 
                 if (path == null) {
                         return;
                 }
 
                 //end of path
                 if (currentWaypoint > path.vectorPath.Count) {
                         if (pathisEnded)
                                 return;
 
                         pathisEnded = true;
                 }
 
         if (pathisEnded == false) {
                         //Direction to next waypoint
                         Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
 
                         dir *= speed * Time.fixedDeltaTime;
 
                         //Move the AI
                         rb.AddForce (dir, fmode);
 
                         float dist = Vector3.Distance (transform.position, path.vectorPath [currentWaypoint]);
 
                         if (dist < nextWaypointDistance) {
                                 currentWaypoint++;
                                 return;
                         }
                 }
                 
         }
 
 
 }
Thanks guys! :). Any help is appreciated!
               Comment
              
 
               
              Note: I only get this error when my enemy is close to the target (so i believe it has no more waypoints to go to)
 
               Best Answer 
              
 
              Answer by hanger · May 18, 2015 at 05:21 AM
Pretty easy to miss.
 if (currentWaypoint > path.vectorPath.Count) {
You mean:
 if (currentWaypoint >= path.vectorPath.Count) {
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                