How to lock vector3 position to the first gameObject that got with in 2500 range?

Hi,
I have a list of gameObject (only 2). I am trying to keep the position the first gameObject that reach within 250 of the A.I… I works great but when the second gameObject comes within 250 of the A.I it changes the position to the second gameObject even with the first gameObject is still within 250.
How to get and lock the position of the first gameobject that gets within 250 doesn’t matter if the second gameObject come within 250 of A.I. and also how to get rid of the position of that particular gameObject once it reach move past a certain limit?

Sorry, if this doesn’t make sense. I tried to explain well

    public GameObject[] Objects; // list of objects

    public static Vector3 closestObj; // Closest Object 
    

    void Start()
    {
    }

    public void FixedUpdate()
    {
        /// <summary> Use to get Object postion and store it as a vairble</summary>


        foreach (var obj in Objects)
        {

                if (Vector3.Distance(transform.position, obj.transform.position) < 250)
                {
                    closestObj = obj.transform.position;
                }

       }

I guess you want something like this:

public GameObject[] Objects; // list of objects

public static GameObject closestObj = null; // Closest Object 
     
public void Update()
{
    if (closestObj == null)
    {
        foreach (var obj in Objects)
        {
            if (Vector3.Distance(transform.position, obj.transform.position) < 250)
            {
                closestObj = obj;
            }
        }
    }
}

This will prevent the AI from choosing another object after it has choosen a target. If you want the AI to loose the target when the current target is further away than 250 you can add an else if statement like this:

if (closestObj == null)
{
    //[ ... ]
}
else if (Vector3.Distance(transform.position, closestObj.transform.position) > 250)
{
    closestObj = null;
}

This would only check the distance to the current target and when the current target goes out of range it resets the closestObj variable and the AI would search for a new target. The same would happen when one of the objects got destroyed. However if you destroy those objects, keep in mind that the references inside the “Objects” array would also become null.