loop is not working

the code is for moving an object from point to point and I want to add a loop and each is not working I was trying with do…while and while (maybe my boolean expression was wrong) this code is with for loop.

public class FollowThePath : MonoBehaviour
{
    //Array of waypoints to walk from one to the next one
    [SerializeField]
    public Transform[] waypoints;

    //Walk speed that can be set in Inspector
    [SerializeField]
    private float moveSpeed = 2f;


    //Index of current waypoint form which Enemy walks to the next one
    private int waypointIndex = 0;

    //Use this for initialization
    private void Start()
    {
        //Set position of Enemy as position of the first waypoint
        transform.position = waypoints[waypointIndex].transform.position;
    }

    //Update is called once per frame
    private void Update()
    {

        for(int i = 0; i <= 10; i++)
        { 
        
            //If Enemy didn't reach last waypoint it can move
            //If Enemy reached last waypoint then it stops
            if (waypointIndex <= waypoints.Length - 1)
            {
                //Move Enemy from current waypoint to the next one
                //using MoveTowards method
                transform.position = Vector2.MoveTowards(transform.position,
                    waypoints[waypointIndex].transform.position,
                    moveSpeed * Time.deltaTime);

                //If Enemy reaches position of waypoint he walked towards
                //then waypointIndex is increased by 1 
                // and Enemy starts to walk to the next waypoint
                if (Vector3.Distance(transform.position, waypoints[waypointIndex].position) < 0.01f)
                {
                    waypointIndex += 1;
                }

                if (waypointIndex == 4)
                {
                    transform.position = waypoints[0].transform.position;
                }
            }
        }
    }
}

I’m not sure why you think you need a loop as Update is essentially a loop as it is called every frame.


I’ve removed the loop and corrected the logic for you;

using UnityEngine;

public class FollowThePath : MonoBehaviour
{
    //Array of waypoints to walk from one to the next one
    [SerializeField]
    public Transform[] Waypoints;

    //Walk speed that can be set in Inspector
    [SerializeField]
    private float _moveSpeed = 2f;

    //Index of current waypoint form which Enemy walks to the next one
    private int _waypointIndex = 0;

    //Use this for initialization
    private void Start()
    {
        //Set position of Enemy as position of the first waypoint
        transform.position = Waypoints[_waypointIndex].transform.position;
    }

    private void Update()
    {
        //If we have no waypoints, get our of here!
        if (Waypoints == null || Waypoints.Length == 0)
            return;
        
        //Move towards the current waypoint
        transform.position = Vector3.MoveTowards(transform.position, Waypoints[_waypointIndex].position, _moveSpeed * Time.deltaTime);

        //If we have reached the waypoint, set the next target
        if (Vector3.Distance(transform.position, Waypoints[_waypointIndex].position) < 0.01f)
        {
            _waypointIndex += 1;

            //We have reached the final waypoint, so we start again
            if (_waypointIndex >= Waypoints.Length)
                _waypointIndex = 0;
        }
    }
}

Oh, yea Update is a loop… I had too big mindfuck I need to rest haha, btw it’s working Thank you!