Remove dead enemies from array

Hello everyone. I’m working on a script that is meant to get all of the game
objects in the scene that are tagged “Enemy”, and then get the closest one to the player. When the enemy is killed and destroyed, the script gives me a missing reference exception error. I’ve tried searching up several possible solutions, such as checking if the enemy is null, but when I try them it usually gives me a warning that states “This method could return default value implicitly.” at (13, 10). Being still very new to unity and things such as lists and arrays, I am unsure how to go about removing an enemy from the array when it is killed without running into these errors. Any helpful advice and references are much appreciated.

Here is the Script :

#pragma strict
var player : Transform;


var enemyNear : boolean;


var targetedEnemy : GameObject;
var closest : GameObject;
var gos : GameObject[];
gos = GameObject.FindGameObjectsWithTag("Enemy");

function FindClosestEnemy() : GameObject
{
 
 var distance = Mathf.Infinity;
 
 for (var go : GameObject in gos)
 {
 	var enemyHealth : EnemyHealth;
 	enemyHealth = go.GetComponent(EnemyHealth);
 	
 	if(enemyHealth.dead == true)
 	{
 		Destroy(go, 3);
 	}
 	
 	if(go != null){
  var diff = (go.transform.position - player.position);
  var curDist = diff.sqrMagnitude;
   if(curDist < distance) 
   {
    closest = go;
    distance = curDist;
   }
  
 			}
 return closest;
 
 }
 }


function EnemyNear()
{
 if(Vector3.Distance(closest.transform.position, player.position) < 10)
  {
   enemyNear = true; 
  } 
 else{
   enemyNear = false;
  }
}



function Start () {
 targetedEnemy = null;
 FindClosestEnemy();
}

function Update () {
 EnemyNear();
 FindClosestEnemy();
 gos = GameObject.FindGameObjectsWithTag("Enemy");
 
}

This is the portion of the EnemyHealth script that the previous script accesses :

function Dead(){
 if(currentHealth <= 0){
  currentHealth = 0;
  dead = true;
 }
 else{
  dead = false;
 }

}

When the enemy is killed, two missing reference exceptions show up,
one at line 46, and one at line 63. Sorry if my post got a little lengthy, thanks for your time.

I would tell you to have the array defined in Update(), so that it always contains only what you tell it to, but I would be giving bad advice. However, I do have good advice. Do some homework on System.Collections.Generic.List, and List.Remove. Also have a look at RemoveAt.

Here’s an example in C# on how to remove an item from a generic list. In this example you’ll need to tag some objects in your scene “Enemy”. When you hit the spacebar, the first index will be removed each time.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class example : MonoBehaviour
{
	public List<GameObject> enemies;
	
	void Awake()
	{
		enemies = new List<GameObject>();
		foreach(GameObject enemy in GameObject.FindGameObjectsWithTag("Enemy"))
		{
			enemies.Add (enemy);	
		}
	}
	
	void Update()
	{
		if(enemies.Count > 0)
		{
			if(Input.GetKeyDown (KeyCode.Space))
			{
				enemies.RemoveAt(0);
				print (enemies.Count);
			}
		}
	}
}