
private var target : Collider = null;
private var targets : Array = new Array();

function Update() 
{
	var targetDist : float = 0;
	target = null;
		
	// Search for nearest target
	for (var item : Collider in targets)
	{
		var v : Vector3 = item.transform.position - transform.position;
		var dist : float = v.magnitude;
			
		if (target == null || dist < targetDist)
		{
			target = item;
			targetDist = dist;
		}
	}
	
	// Draw line to nearest target
	if (target)
	{
		Debug.DrawLine(
			transform.position, 
			target.transform.position, 
			Color(0, 1, 0));
	}
}

function OnTriggerEnter(other : Collider)
{
	// Add collider to array
	targets.Push(other);
}

function OnTriggerExit(other : Collider)
{
	// Remove collider from array
	var index : int = 0;
	for (var item : Collider in targets)
	{
		if (item == other)
		{
			targets.RemoveAt(index);
			break;
		}
		index += 1;
	}
}