The whole script:
public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
[SerializeField] float moveSpeed = 2f; //2
int waypointIndex = 0; //1
//only looking for the transform information from the waypoints
void Start()
{
transform.position = waypoints[waypointIndex].transform.position; //3 4
}
void Update()
{
Move();
}
private void Move()
{
if (waypointIndex < waypoints.Count) //5
{
Debug.Log("1");
var targetPosition = waypoints[waypointIndex].transform.position; //6 7
var movementThisFrame = moveSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards //8
(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition) //9
{
Debug.Log("2");
waypointIndex++;
}
//else
if (waypointIndex == waypoints.Count)
{
Debug.Log("3");
Destroy(gameObject);
}
}
}