Check distance between all objects on a list?

I’m trying to make it so an object checks how far it is from any object from a list. I have this script so far but it seems to only work with the closest one and ignores all others

public var objects : GameObject[];
private var cm : CharacterMotor;
private var nearObject : boolean;
static var slow : boolean;

function Start () {
	cm = gameObject.GetComponent("CharacterMotor") as CharacterMotor;
	objects = GameObject.FindGameObjectsWithTag("Tree");
}
     
function Update () {
	for (var object in objects) {
		if ((transform.position - object.transform.position).magnitude < 10) {
			nearObject = true;
		} else {
			nearObject = false;
		}	
	}
	
	if (nearObject == true && slow == false) {
		cm.movement.maxForwardSpeed = 12;
		cm.movement.maxSidewaysSpeed = 10;
		cm.movement.maxBackwardsSpeed = 8;
	} 
	
	if (slow == true) {
		cm.movement.maxForwardSpeed = 6;
		cm.movement.maxSidewaysSpeed = 5;
		cm.movement.maxBackwardsSpeed = 4;
	}
	
	if (slow == false && nearObject == false) {
		cm.movement.maxForwardSpeed = 10;
		cm.movement.maxSidewaysSpeed = 8;
		cm.movement.maxBackwardsSpeed = 6;
	}
}

Printing the distance only tracks closest object on map, not sure if it’s closest or last on the list as someone pointed out it’s “last”

I’m not very familiar with UnityScript but “object” is a reserved word in C# (I was noticing the syntax color in your example). I would try this instead:

for (var obj : GameObject in objects) {
    if ((transform.position - obj.transform.position).magnitude < 10) {
}

Hi, did you tried the Vector3.Distance() function?

if (Vector3.Distance(transform.position, object.transform.position) < 10) 
{
     //Do something
} 
else 
{
     //Do something else
}