My Enemy prefab is not being destroyed when reaching the last waypoint

Hi guys!

I think I followed the lesson correctly but Unity is giving me an error when its time to destroy the enemy prefab:

I’m using Unity 2020.1.17f1


image

public class EnemyPathing : MonoBehaviour
{
    [SerializeField] List<Transform> waypoints;
    [SerializeField] float moveSpeed = 2f;
    int waypointIndex = 0;

    private void Start()
    {
        transform.position = waypoints[waypointIndex].transform.position;
    }

    private void Update()
    {
        Move();
    }

    private void Move()
    {
        var targetPosition = waypoints[waypointIndex].transform.position;
        var movementThisFrame = moveSpeed * Time.deltaTime;
        if (waypointIndex <= waypoints.Count - 1)
        {

            transform.position = Vector2.MoveTowards(transform.position, targetPosition, movementThisFrame);
            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

If you click on the error it should give you the line of code and class in which the error occured. That would be helpful.

I’m think the error is thrown by this line of code:

var targetPosition = waypoints[waypointIndex].transform.position;

if you check the error type, it says “Argument out of Range”, and that it’s an index that’s out of range.

If you go through what your code does step by step you can tell what’s going on. Let’s assume your current waypointindex is 2, so you’re on the last waypoint.

var targetPosition = waypoints[2].transform.position; // because your list of waypoints has 3 objects this works
var movementThisFrame = moveSpeed * Time.deltaTime;

next we go into the if statement.

if (2 <= 3 - 1) // translating into the corresponding numbers here. 2 is equal to 2, so we go into this if statement.

next you set the target position and move the object towards. We can ignore that part. Your next if is where it get’s interesting.

if (transform.position == targetPosition) //once you reach the goal, you go into this if statement
            {
                waypointIndex++; //alright, so your waypointIndex goes from 2 to 3
            }

Now everything in move happened, so let’s go to the next frame.

It get’s called again. Remember that our waypointIndex is now 3 already

var targetPosition = waypoints[3].transform.position; /*this is where the error is 
throwing, because you don't have an object at index 3 because the 
list only has 3 elements.*/

So you need to make sure that you don’t execute the line that throws the error, if your currentWaypointIndex is bigger than the highest index in the list.
If you already got an idea for how to fix it, ignore the next bit, otherwise, here’s what should fix it:

var targetPosition = waypoints[waypointIndex].transform.position; // move this line into the if statement.
var movementThisFrame = moveSpeed * Time.deltaTime;
if (waypointIndex <= waypoints.Count - 1)
        {
1 Like

Thanks bro! That worked correctly! You’re saving me beetwen the yesterday’s post and this one hahaha ^_^!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms