Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

trying to make object follow waypoints and come back

Discussion in 'Scripting' started by brunoenvia, Dec 11, 2019.

  1. brunoenvia

    brunoenvia

    Joined:
    Aug 5, 2019
    Posts:
    52
    i'm getting an out of array with this i dont understand why

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. public class Path : MonoBehaviour
    8. {
    9.     [SerializeField] List<Transform> wayPoints;
    10.     [SerializeField] float moveSpeed = 2f;
    11.     int waypointIndex = 0;
    12.  
    13.     Transform initialPosition;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.        
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (waypointIndex <= wayPoints.Count) // if waypointIndex is less or equal to the total of elements in the list wayPoints - 1
    25.         {          
    26.  
    27.             var targetPosition = wayPoints[waypointIndex].transform.position; // targetPosition is the list of type Transform containing the waypoints | it starts at index waypointIndex which is = 0
    28.             var movementThisFrame = moveSpeed * Time.deltaTime; // gives movement speed
    29.             transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame); // this object position will move towards the targetPosition with movementThisFrame speed.
    30.        
    31.  
    32.          if(waypointIndex <= wayPoints.Count) // if the position of this object is equal to the targetPosition
    33.          {
    34.                 waypointIndex += 1; // increase index of waypointIndex by 1 moving towards the waypoint with the respective index.
    35.                
    36.                
    37.          }
    38.         }
    39.  
    40.  
    41.     }
    42. }
    43.  
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    827
    Hi,
    Without testing it, I make a guess that it might be that your List does not contain enough items.
    See your code: Now you test in the outer If condition, is waypointIndex less or equal than the wayPoints count?
    What if the wayPointIndex is zero and wayPoints count is zero, too? (The list will return count zero even if it is totally empty, but is initialized.)

    So, your code can proceed even if there's no items in the array and runs into error situation when you try to get the non-existent target position transform.

    Also, don't test less or equal when you check for the length of an array - think about this: if your wayPoint index was 5, and your wayPoints count was 5, then you would already be 1 over the array size, as it starts from zero index. (0,1,2,3,4). So, just test for less (<) in this case.

    And it's simple to test if something is valid object or at least is non-null, so do that before actually trying to blindly assign it as a value.

    Here's functioning code:
    Code (CSharp):
    1. if (waypointIndex < wayPoints.Count)
    2. {
    3.     if (wayPoints[waypointIndex] != null)
    4.     {
    5.       var targetPosition = wayPoints[waypointIndex].transform.position;
    6.       var movementThisFrame = moveSpeed * Time.deltaTime;
    7.       transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementThisFrame);
    8.     }
    9.  
    10.     if (waypointIndex <= wayPoints.Count)
    11.     {
    12.       waypointIndex += 1;
    13.  
    14.  
    15.     }
    16. }
    Just as a general tip, always try printing out everything and anything when testing things, that way you realize how things function in edge case situations. Those objects might not actually do what you think. And this usually leads to errors in code.
     
unityunity