tageting system 3d space

Hi in my space game i want to have a tab to next enemy but i cans seem to get the tab to work on my script i have the list showing my 3 baddys but when i hit tab there is noting going on any help would be amazing here is the code i have so far for it.

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

private Transform myTransform;

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

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));
			});
		
}

private void TargetEnemy()
{
	if(selectedTarget = null)
	{
		SortTargetsByDistance();
		selectedTarget = targets[0];
	}
	
}

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

}

Your code seems to select the closest target properly. You should use sqrMagnitude instead of the magnitude btw, no need to calcul the sqrt for comparison.

However, you never do anything with that selection. Do you want to use LookAt on it ?

i think my problem lies in the private void sortTargetsByDistance. I thought I set that up for a 3d environment, but it seems to only see things when I line the ship up with them at the same Y. What changes do I need to make so that it will see all the “enemy” tags in the world in the world? Also yes im going to use a LookAt function and create an orbit script for the space combat and docking.