Reset closest gameObject after it gets destroyed

Hello guys,
I know this question has been asked, but no solution works for me so far.
Here is what I got:

I want a AI Unit seek for the closest Enemy, walking towards and killing it.
I used the example Script from the Unity Documentation, which works so far.
But when the Unit kills the 1st Enemy it wont refresh the closest target value.
Here is my code:

var targetEnemy : GameObject[];
var closest : GameObject;
var distance = Mathf.Infinity;
var base : GameObject;

function Update () {

	findEnemy();
		
	if (closest != null){
		kill();
	}
}

function findEnemy () : GameObject {

	targetEnemy = GameObject.FindGameObjectsWithTag("Enemy");
	
	for (var go : GameObject in targetEnemy)  { 
			var diff = (go.transform.position - transform.position);
			var curDistance = diff.sqrMagnitude; 
			if (curDistance < distance) { 
				closest = go; 
				distance = curDistance; 
			} 
		} 
		return closest;
}

function kill (){
	GetComponent(NavMeshAgent).destination = closest.transform.position;
}

Hope you guys can help me once more and thank you in advanced.

Greetings,
Nitrosocke

You are not resetting distance. So if one enemy gets to a range of 10 and you kill it, the next enemy must get to a range of 9 before it can be set as closest, and the next one to 8 etc…

Making distance a local variable should fix this

function findEnemy () : GameObject {
 
     targetEnemy = GameObject.FindGameObjectsWithTag("Enemy");
     var distance = Mathf.Infinity;

     for (var go : GameObject in targetEnemy)  { 
         var diff = (go.transform.position - transform.position);
         var curDistance = diff.sqrMagnitude; 
         if (curDistance < distance) { 
             closest = go; 
             distance = curDistance; 
         } 
     } 
     return closest;
 }