Simple patrol enemy issue

Sorry this may be stupid, but I ant figure this out, no matter what tutorial I follow, no matter how many attempts I try to create a simple enemy patrol system. I wanted it to be a bit more reusable but I cant get anything to work, no errors just not working, and yes my enemy is attached. Here is my code! Also, please dont slaughter me for syntax, or for being a beginner, etc, I got enough hate from SO.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    public GameObject Enemy;
    public Vector3 targetA = new Vector3(23, 3, 0);
    public Vector3 targetB = new Vector3(23, -3, 0);
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Enemy.transform.position.y !>= targetA.y)
        {
            Enemy.transform.position += new Vector3(0, 1, 0) * Time.deltaTime * speed;
        }
        
    }
}

P.S.
The reason Point b exists is because I would use that next, but I cant get point a to work in the first place.

I recommend you to visualize the sequence of events and write all the steps down, what actually happens. This way you can get the pseudo code worked out (= what EXACTLY should happen) and then you can work out the details.

But anyway, try to break this apart (I put some comments, but remove those and go through the code) and see how it works.

You’ll have to drop your waypoint Transforms into list of the scripts and also drop your enemy agent transform to its slot in script. This should make your enemy partrol between points, and when it reaches the last point of the path, it will set the first point as the next target point.

public class EnemyPatrol : MonoBehaviour
{
    [SerializeField] List<Transform> targetPoints;
    [SerializeField] Transform agentTra;
    [SerializeField] float moveSpeed = 2f;

    private Vector3 currentTarget;
    private int currentIndex;
    
    void Start()
    {
        // set next point, expect to have 
        // at least to points in list
        currentIndex = 1;
        currentTarget = targetPoints[currentIndex].position;
    }

    void Update()
    {
        // If not close enough to target point
        // move towards it
        // Must be closer than 0.1m to target
        if (Vector3.Distance(agentTra.position, currentTarget) > 0.1f)
        {
            agentTra.position = Vector3.MoveTowards(agentTra.position, currentTarget, moveSpeed * Time.deltaTime);
        }

        // reached current target point
        // get next point
        // if no next in list, get the first one
        else 
        {
            currentIndex++;

            if (currentIndex >= targetPoints.Count)
                currentIndex = 0;

            currentTarget = targetPoints[currentIndex].position;
        }
    }
}

I would also recommend not using expressions like !>= as it is really mind-boggling to read :slight_smile: