GetClosestObject function not working as intended?

Hello all, i am making a GetClosestObj function in order to get the closest gameobject in a given direction. It takes two arguements, an array of objects to check, and an int, either -1 or 1, to decide whether to check to the left or to the right. Below is the code:

   GameObject GetClosest(GameObject[] sceneObjects, int direction)
    {
        float closest = float.MaxValue;
        GameObject closestObj = null;

        if(direction == -1)
        {
            closest = float.MinValue;
            for(int i = 0; i < sceneObjects.Length; i++)
            {
                float xPosition = sceneObjects[i].transform.position.x;
                if(Vector3.Distance(sceneObjects[i].transform.position, movingreticle.transform.position) != 0)
                {
                    if (xPosition > closest)
                    {
                        closest = xPosition;
                        closestObj = sceneObjects[i];
                    }
                }
            }
        }
        else if(direction == 1)
        {

        }

        return (closestObj);
    }

the code to check to the right is not in yet, as once the left is completed, i will copy-paste the code to check to the left and reverse the direction to check in. In any case, the problem line comes here: if(Vector3.Distance(sceneObjects[i].transform.position, movingreticle.transform.position) != 0)
there are three gameobjects in the scene, and the “movingreticle” always starts on the rightmost one. when this part is called, the movingreticle goes from the rightmost to the one in the middle as it should, however, calling this again when the movingreticle is on the middle one instead takes it back to the right. However, without this check, the reticle will always stay in one place, as the ClosestObject would otherwise be the one it’s already on top of.

bump

bump