Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by TheForsaken95, Jun 21, 2015.

  1. TheForsaken95

    TheForsaken95

    Joined:
    Aug 29, 2014
    Posts:
    30
    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
    Code (CSharp):
    1.         if (targetObject != null) {
    2.             target = pathfinder.BeginPathing (transform, targetObject);
    3.         }
    And the path-finding code:
    Code (CSharp):
    1.  
    2.     [SerializeField] float radius;
    3.     private List<Vector3> waypoints = new List<Vector3>();
    4.     private List<Vector3> path = new List<Vector3> ();
    5.     private int pathLayer;
    6.     private int blockLayer;
    7.     private Transform source;
    8.     private Transform target;
    9.     private Transform nextWaypoint;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         pathLayer = 1 << 9;//LayerMask.NameToLayer("Pathfinding");
    14.         blockLayer = 1 << 10;
    15.     }
    16.  
    17.     public Vector3 BeginPathing(Transform sourceObject, Transform targetObject) {
    18.         source = sourceObject;
    19.         target = targetObject;
    20.         if (target != null) {
    21.             Collider[] hit = Physics.OverlapSphere (sourceObject.position, radius, pathLayer);
    22.             if (hit != null) {
    23.                 foreach (Collider waypoint in hit) {
    24.                     //Generate list of waypoints.
    25.                     waypoints.Add (waypoint.transform.position);
    26.                 }
    27.                 if(path == null) {
    28.                     FindPath();
    29.                     return path[0];
    30.                 } else {
    31.                     if(sourceObject.position == path[0]) {
    32.                         path.RemoveAt (0);
    33.                     }
    34.                     return path[0];
    35.                 }
    36.             } else
    37.                 return Vector3.zero;
    38.         } else {
    39.             return Vector3.zero;
    40.         }
    41.  
    42.     }
    43.  
    44.  
    45.     void FindPath() {
    46.         RaycastHit hit;
    47.         bool success = false;
    48.         path.Add (source.position);
    49.         for (int i = 0; i < path.Count; i++) {
    50.             if (!Physics.Linecast (path[i], target.position, out hit, blockLayer)) {
    51.                 //If there is nothing between me, and the target object.
    52.                 path.Add (target.position);
    53.                 success = true;
    54.                 break;
    55.             }
    56.             for(int j = 0; j < waypoints.Count; j++) {
    57.                 if (!Physics.Linecast (path[i], waypoints [j], out hit, blockLayer)) {
    58.                     //If there is nothing between the current waypoint, and the target waypoint.
    59.                     path.Add (waypoints[j]);
    60.                 }
    61.             }
    62.         }
    63.     }
    64. }
    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.
     
    Last edited: Jun 21, 2015
  2. TheForsaken95

    TheForsaken95

    Joined:
    Aug 29, 2014
    Posts:
    30
    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.
     
    NomadKing likes this.