c# how to refresh my targeting list

my targeting script gets a list of enemies i can target but once i kill one the script dose not update the list and says missing game object making it impossible to target and attack and kill the other enemies

here is my targeting script

///
/// TargetMob.cs
/// This script can be attached to any permanent gameobject, and is responsible for allowing the player to target different mobs that are with in range
///
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Targetting : MonoBehaviour {
public List targets;
public Transform selectedTarget;

private Transform myTransform;
	

// Use this for initialization
void Start () {
	targets = new List<Transform>();
	selectedTarget = null;
	myTransform = transform;
	
	AddAllEnemies();
	
}


public void AddAllEnemies() {
	GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
	
	foreach(GameObject enemy in go)
		AddTarget(enemy.transform);
}

public void AddTarget(Transform enemy) {
	targets.Add(enemy);
}


private void SortTargetsByDistance() {
	targets.Sort(delegate(Transform t1, Transform t2) {
			return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
			});
}

//if we do not have an enemy targeted ywt, then find the clostest one and target him
//if we do have an enemy targeted, then get the next target
//if we have the last target in the list, then get then first target in the list
private void TargetEnemy() {
	if(selectedTarget == null) {
		SortTargetsByDistance();
		selectedTarget = targets[0];
		

	}
	else {
		int index = targets.IndexOf(selectedTarget);
		
		if(index < targets.Count - 1) {
			index++;
		}
		else {
			index = 0;
		}
		DeselectTarget();
		selectedTarget = targets[index];
	}
	SelectTarget();
}

private void SelectTarget() {
	selectedTarget.renderer.material.color = Color.red;
	
	PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
	
	pa.target = selectedTarget.gameObject;
}

private void DeselectTarget() {
	selectedTarget.renderer.material.color = Color.white;
	selectedTarget = null;
}

// Update is called once per frame
void Update () {
	if(Input.GetKeyDown(KeyCode.Tab))
		TargetEnemy();
}

}

I would suggest having the script that controls your enemy send a message to the TargetMob script when they die. I expect the Mob script will already have a reference to the player so I suggest attaching the TargetMob script to the player object.
Then, in the Mob script:

player.SendMessage("RemoveTarget", transform);

(Obviously replacing ‘player’ with whatever your variable is called.

Then, in TargetMob add a new method like this:

public void RemoveTarget(Transform target) {
    targets.RemoveAt(targets.IndexOf(target));
}

I hope this solves your problem

You’d be better off doing that in a different way. Rather than finding the enemies like you do, attach a script to each enemy that records it in an available list and removes itself when destroyed.

Here is my base class I use for things like this:

 using System.Collections.Generic;
 using UnityEngine;

public class MonoCollection<T> : MonoBehaviour where T : MonoCollection<T>
{
    public static List<T> all = new List<T>();
	
	protected virtual void OnEnableEx()
	{
	}
	
	protected virtual void OnDisableEx()
	{
	}
	
    protected  void OnEnable()
    {
        all.Add(this as T);
		OnEnableEx();
    }
	
    protected  void OnDisable()
    {
        all.Remove(this as T);
		OnDisableEx();
    }
}

So lets say you make an Enemy class

    public class Enemy : MonoCollection<Enemy>
    {
           public string type;

           //Presuming you want functionality in OnEnable write an override 
           //of OnEnableEx() or call the base function
           new void OnEnable()
           {
               base.OnEnable(); // Make sure to call the base if you write your own OnEnable
           }
    }

Now you can get your enemies like this:

       foreach(var enemy in Enemy.all)

Note that if you are sorting based on distance your method is significanly slower than using Linq and OrderBy. This is due to the fact that .Sort needs to call the predicate for each comparison which uses as Sqrt in your case, Linq only calls each one once. The fastest way to write that sort is:

    using System.Linq;

    ...

    var myPosition = transform.position;  
    var sortedOrder = Enemy.all.OrderBy(e=>Vector3.Distance(e.transform.position, myPosition); //.ToArray() or .ToList() if you need them all
    var closest = sortedOrder.FirstOrDefault();

Now it’s even faster if you write a Linq extension for MinBy, then it’s just a single pass through the list:

	public static T MinBy<T,TR>(this IEnumerable<T> source, Func<T, TR> clause, IComparer<TR> comparer=null)
	{
                    T result = default(T);
                    TR min = default(TR);
                    bool doneOne;
                    IComparer<TR> = Comparer<TR>.Default;
                    foreach(var item in source)
                    {
                        var current = clause(item);
                        if(!doneOne || comparer.Compare(min, current) > 0)
                        {
                             min=current;
                             result = item;
                             doneOne = true;
                        }
                    }
                    return result;
            }

Now you can do:

     var myPosition = transform.position;
     var closest = Enemy.all.MinBy(e=>Vector3.Distance(e.transform.position, myPosition);