targeting multiple enemys

I am using the OverlapSphere to get enemies for my targeting system.

here is my code

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using UnityEngine;
using System.Collections;

public class advancedTargetting : MonoBehaviour {
	
	
		public bool showRing = false;
	private Transform myTransform;
	public float strafeSpeed = 10;
	public GameObject player;
	public bool look;
	Transform selected = null;
	
	public float range;
	// Use this for initialization
	
		public void Awake() {
		

		
		
		
		myTransform = transform;
	
		
		animation["CombatStance"].wrapMode = WrapMode.Loop;
		
		
	}
	
	
	void Start () {
	  
		
		
	
	}
	
	public Transform FindNearestEnemyInSphere(float radius){ 
    float minDist = Mathf.Infinity;
    Transform nearest = null;
    Collider[] cols = Physics.OverlapSphere(transform.position, 10);
    foreach (Collider hit in cols) {
        if (hit && hit.tag == "Enemy"){
            float dist = Vector3.Distance(transform.position, hit.transform.position);
            if (dist < minDist){
                minDist = dist;
                nearest = hit.transform;
					
		
            }
        }
    }
		
		
    return nearest;
}
	
	// Update is called once per frame
	

	
		private void SortTargetsByDistance() {
		
	}

   
     void Update() {
		
		
		 if (Input.GetKeyDown("tab")){
        if (selected == null){ // if nothing selected...
            Transform enemy = FindNearestEnemyInSphere(range);
			Transform enemyt = FindNearestEnemyInSphere(7);
            if (enemy){
                selected = enemy;   
			turnObjectOnandOff a = enemy.GetComponent<turnObjectOnandOff>();
        a.ring.active = true;
				look = true;
            }
				
				
        } else {	
				
				showRing = false;
		  turnObjectOnandOff a = selected.GetComponent<turnObjectOnandOff>();
        a.ring.active = false;
					look = false;
            selected = null; // then clear the variable
        }
			
    }
		
		
		
		

		
		
	

}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The problem is that it does not sort by distance. it just gets the one closest to you. How would I add sort by distance in this script?

There are a couple of things here- you’d need to restructure the way the enemy finder works, since at the moment it can only return a single enemy! You first need to make it return an array or a list (preforably a List, for simplicity’s sake), then populate the list in the function.

First up, put ‘using System.Collections.Generic’ at the top, so that you have generics (which are really useful).

public IList<Transform> FindEnemiesInSphere(float radius){ 
    Collider[] cols = Physics.OverlapSphere(transform.position, 10);
    SortedList<float, Transform> transforms = new SortedList<float, Transform>();
    foreach (Collider hit in cols) {
        if (hit && hit.tag == "Enemy"){
            float Dist = Vector3.Distance(transform.position, hit.transform.position);
            transforms.Add(dist, hit.transform); 
        }
    }
    return transforms.Values;
}

This will give you a list of Transforms which you can iterate through with a Foreach, and which will be sorted in order of distance!