using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.AI;
public class wps : UnityEngine.MonoBehaviour
{
public List<Transform> waypoints = new List<Transform>();
public float speed = 2.0f;
public bool faceHeading = true;
public bool loop = false;
private int index = 0;
// Use this for initialization
protected void Start()
{
var ways = GameObject.FindGameObjectsWithTag("wp");
foreach (GameObject go in ways)
{
waypoints.Add(go.transform);
}
}
private void Update()
{
if(index != waypoints.Count)
{
transform.position = Vector3.MoveTowards(transform.position, waypoints[index].position, speed * Time.deltaTime);
}
if(transform.position == waypoints[index].position)
{
index++;
}
}
}
The object is getting to the last way point but then show the error ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index on line 33
if(transform.position == waypoints[index].position)