Find Closest Target, they all find same object

I have this script attached to my friendlyUnits, one timeToAttack is true they move towards to same enemyUnit and believe me one enemyUnit is closer than the other one to my second friendlyunit.

 GameObject closest;
    PlayerControls controls;
    float speed = 7;
    bool foundControls;
    void Start()
    {
        foundControls = false;
        FindClosestEnemy();
        controls = GameObject.FindGameObjectWithTag("GamePlay").GetComponent<PlayerControls>();
    }

   
    void Update()
    {
       
        if (controls.timeToAttack == true)
        {
            float step = speed * Time.deltaTime;
           gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, closest.transform.position, step);
        }
    }

    #region findClosest
    GameObject FindClosestEnemy()
    {
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Player");

        float distance = Mathf.Infinity;
        Vector3 position = transform.position;
        foreach (GameObject go in gos)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }

        if(closest!=null)
        Debug.Log(closest.name);

        return closest;
    }
    #endregion

Are you sure all of the objects have the correct tag on them? (Try Debug.Log(“Found “+gos.Length+” objects”); after line 28 to make sure it’s finding the expected number of objects)

Yes it finds all 3 units but still 2 of them move towards to same one “(fri) = = (enm)” they are placed like this

One thing I notice is that you call FindClosestEnemy during Start, so whichever one is closest at the instant the object is created would be the one it finds. Do these objects, or the targets, ever move? If so, you’ll probably want to call FindClosestEnemy when timeToAttack is first set to true.

yes it worked well thanks, now trying to figure out why this is only workin on unity play but not on build for multiplayer