Closest game object script not working C#.

This is so annoying, i have no idea why its not finding closest object and setting it as “closest”. Can someone please tell me whats wrong with this.

for(int i = 0; i < weaponsOnGround.Count;i++){
                distance = Mathf.Abs(transform.position.x + transform.position.z) - Mathf.Abs(weaponsOnGround[i].transform.position.x - weaponsOnGround[i].transform.position.z);
                Checking = weaponsOnGround[i];
                if(closest == null)closest = Checking;
                if (distance < Mathf.Abs(closest.transform.position.x + closest.transform.position.z)) closest = Checking;
            }

Hi,
Calcul distance with the function Vector.Distance(). This should be work

Vector2.Distance(vec1, vec2)

I’m using vector3’s and i cant compare vectors with < or >.

I’m not certain but I think you can use the following to get the distance.

float distance = (Vector1 - Vector2).magnitude;

Vector2.Distance returns a float

This?