Need help with a Third-Person Styled Lock-On System

I want to implement a Lock-on/Trigger system that will let the player find the closest enemy, have the player facing the enemy, and having him attack towards the enemy he is targeted to. I also want to display a ring or a targeting icon on the enemy that is targeted.

How can I make it to where I press the targeting button and have it select the closest enemy and make it "the target"?

I had suggestions to where I give the enemies tags called "Enemy" (which i did) and store them into an array, but that wasn't really enough help :P

Also, after defeating the enemy that I manually set as the target in the inspector, I get this error

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.

because of the code that allows the player to face the enemy when attacking:

transform.LookAt(Vector3(theTarget.transform.position.x, transform.position.y, theTarget.transform.position.z));

I think 'Transform' is referring to 'theTarget' being destroyed

How do I get the script to check if "theTarget" is null after destroying it or how do I disable the LookAt() line if enemies don't exist on the map?

Solved everything! (Not by myself entirely, this is a bit embarrassing)

This is the script I wrote thanks to handsomePATT from the forums and an answer skovacs1 gave a while back to another answer (got to give credit where credit is due)

//Lock On Script
private var current : int = 0; 
private var locked : boolean = false; 
var playerController : ThirdPersonController ;
var enemyLocations : GameObject[];
var closest : GameObject;
var activeIcon : Transform; //Current targeted enemy indicator

function Update() 
    var playerController : ThirdPersonController = GetComponent(ThirdPersonController);

    if (closest != null && locked)
{ = true;
activeIcon.transform.position.y = (closest.transform.position.y+1);
activeIcon.transform.position.x = (closest.transform.position.x);
activeIcon.transform.position.z = (closest.transform.position.z);
{ = false;

    //Looks for the closest enemy
    locked = !locked;

        //If there aren't any enemies (or the player killed the last one targeted) make sure that the lock is false
        if (!closest)
   = false;
            locked = false;
            closest = null;

        if (playerController.isAttacking)
        transform.LookAt(Vector3(closest.transform.position.x, transform.position.y, closest.transform.position.z));

function FindClosestEnemy () : GameObject 
    // Find all game objects with tag Enemy
    enemyLocations = GameObject.FindGameObjectsWithTag("Enemy"); 
    //var closest : GameObject; 
    var distance = Mathf.Infinity; 
    var position = transform.position; 
    // Iterate through them and find the closest one
    for (var go : GameObject in enemyLocations) 
            var diff = (go.transform.position - position);
            var curDistance = diff.sqrMagnitude; 

            if (curDistance < distance) 
                closest = go; 
                distance = curDistance; 
    return closest; 


Not Actually coming up with a ready answers ( sorry :) ), but just some directions that might be useful.

It looks to me you need to add another hierarchy level between the controller and the player by adding one gameObject inbetween the two, that gameobject will then allow to control the rotation independently from the controller.

This allow you to have the controller moving and looking at its target while you have the inbetween gameObject looking somewhere else.

Anyway, I think for someone to really come up with an answer, you should make a small example, exposing the issue, then I would happily have a look, mess with it to achieve the desired effect. This is the most effective way in my opinion to solve such precise case.