Attack only targeted enemies Unity
<p>I have a script which is used to control targeted enemies and another to control player attacks, the targeting script seems to work fine on its own until the first target is killed, so when I click Tab it targets my enemies in distance order but when my first target dies it cannot target another and I get the message: </p>

<blockquote>
  <p>MissingReferenceException: The object of type 'Transform' has been
  destroyed but you are still trying to access it. Your script should
  either check if it is null or you should not destroy the object.</p>
</blockquote>

<p>Also I am unable to attack anything except the first player whether it is targeted or not. I tried switching my playerattacks so it worked with a list just like my targeting script however I get all the same errors.</p>

<p>My targetting script is:</p>

<pre><code>public class Targetting : MonoBehaviour {

public List&lt;Transform&gt; targets;
public Transform selectedTarget;
public string targetTag = "Enemy";
private Transform myTransform;

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

public void AddAllEnemies() {
    GameObject[] go = GameObject.FindGameObjectsWithTag(targetTag);     
    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));
    });
}

private void TargetEnemy() {
    if(selectedTarget == null) {
        SortTargetsByDistance();
        selectedTarget = targets[0];
    } else {
        int index = targets.IndexOf(selectedTarget);    
        if (index &lt; targets.Count -1) {
            index++;
        } else {
            index = 0;    
        }

        selectedTarget = targets[index];
    }
}

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

<p>And my PlayerAttack Script is currently:</p>

<pre><code>public class PlayerAttack : MonoBehaviour {

public enemyHealth eHealth;
public GameObject enemy;
private float MHCD; // MeleeHit CD

private float hitChance;

// Use this for initialization
void Start () {

}

void Update () {
    hitChance = Random.Range (1, 100); 

    if (Input.GetKeyDown (KeyCode.Alpha1) &amp;&amp; Time.time - MHCD &gt; 1) { 
        if (hitChance &gt; 0 &amp;&amp; hitChance &lt; 90) {
            Attack1 (); 
            MHCD = Time.time; 
        }
    }
}  

void Attack1() {
    float distance = Vector3.Distance (enemy.transform.position, transform.position);

    if (distance &lt; 4) { 
        eHealth.SendMessage ("MeleeHit");
    } else {
        Debug.LogWarning ("You are too far away!"); 
    }
}
}
</code></pre>

<p>Basically what I am looking to do is to make both scripts work with eachother so when an enemy is targeted, it'll only deduct damage from the targeted enemy, and when an enemy is killed it is removed from the list 'targets'</p>