RTS Style Selection Not Working

Hello, I made a selection system and it messes up in a weird way that I don’t really get haha.
Here’s my script,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BimbusMove : MonoBehaviour
{
    Ray camRay;
    RaycastHit hit;
    List<Transform> selectedBimbi = new List<Transform>();
    bool isDragging = false;
    Vector3 mousePosition;

    private void OnGUI()
    {
       
            if (isDragging)
            {
                var rect = ScreenHelper.GetScreenRect(mousePosition, Input.mousePosition);
                ScreenHelper.DrawScreenRect(rect, new Color(0.8f, 0.8f, 0.95f, 0.1f));
                ScreenHelper.DrawScreenRectBorder(rect, 1, Color.blue); 
            }    
    }
   


    public void MoveBimbus(Vector3 dest)
    {

    }
    public void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            mousePosition = Input.mousePosition;
            camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(camRay, out hit))
            {
                if (hit.transform.CompareTag("Bimbi"))
                {
                    SelectBimbus(hit.transform, Input.GetKey(KeyCode.LeftControl));
                }
                else
                {
                    isDragging = true;
                }

            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            if (isDragging)
            {
                DeselectBimbus();

                foreach (var selectableObject in FindObjectsOfType<BoxCollider>())
                {
                    if (IsInSelectBox(selectableObject.transform))
                    {
                        SelectBimbus(selectableObject.transform, true);
                        print(selectedBimbi);
                    }

                }
               
            }
            isDragging = false;
        }

    }
    private void SelectBimbus(Transform unit, bool isMultiBimbi = false)
    {
        if (!isMultiBimbi)
        {
            DeselectBimbus();
        }
            selectedBimbi.Add(unit);
            unit.Find("Highlight").gameObject.SetActive(true);               
    }
    private void DeselectBimbus()
    {
      
        for(int i = 0; i < selectedBimbi.Count; i++)
        {
            selectedBimbi[i].Find("Highlight").gameObject.SetActive(false);
           
        }
        selectedBimbi.Clear();
    }
    private bool IsInSelectBox(Transform transform)
    {
        if(!isDragging)
        {
            return false;
        }
        var camera = Camera.main;
        var viewportBounds = ScreenHelper.GetViewportBounds(camera, mousePosition, Input.mousePosition);
        return viewportBounds.Contains(camera.WorldToViewportPoint(transform.position));
       
    }

}

So, the individual selection works just fine, but I have two major issues. One, if I zoom out the camera the selection box does not disappear. Every time I click it just redraws itself and I get an error at line 84 saying object reference not set to an instance of an object, even though this error does not come up when I don’t zoom the camera :confused: the weirdest part is the error doesn’t come up right away, it only comes up after a few drags.

Another issue is, it doesn’t seem to be selecting the units at all. If I point and click, it selects, but if I use a drag box, it doesn’t select at all.

Any help would be appreciated!

One problem (among many) with .Find is that it only finds active GameObjects. Since you’re activating and deactivating these things, you’ll routinely be unable to .Find the highlight objects, causing your error. Rather than using Transform as your type, use a class of your own that you’ve added to every unit, and add a “public GameObject highlight” field to that script, which you set in the unit’s inspector. That reference will always be valid even if the object is deactivated.

You’re using OnGUI to display your selection rectangle, which is the old and deprecated UI system. You’ll be much better off learning to use the modern UI, UnityUI. It’s possible that the coordinate system which OnGUI uses might not have the numbers you’d expect, so it may just be “selecting” the wrong area of the screen. good news is your code is not far from what you’d need to make it work with UnityUI - here’s a tutorial that should give you an idea of how to use it for this.

1 Like

Thanks, very helpful. I followed the tutorial and this is what I got for the selection box.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelectBox : MonoBehaviour
{
    [SerializeField]
    private RectTransform selectSquareImg;

    Vector3 startPos;
    Vector3 endPos;
    void Awake()
    {
        selectSquareImg.gameObject.SetActive(false);
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;

            if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
            {
                startPos = hit.point;
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            selectSquareImg.gameObject.SetActive(false);

        }
        if (Input.GetMouseButton(0))
        {
            if (!selectSquareImg.gameObject.activeInHierarchy)
            {
                selectSquareImg.gameObject.SetActive(true);
            }

            endPos = Input.mousePosition;

            Vector3 squareStart = Camera.main.WorldToScreenPoint(startPos);
            squareStart.z = 0f;

            Vector3 centre = (squareStart + endPos) / 2f;

            selectSquareImg.position = centre;

            float sizeX = Mathf.Abs(squareStart.x - endPos.x);
            float sizeY = Mathf.Abs(squareStart.y - endPos.y);

            selectSquareImg.sizeDelta = new Vector2(sizeX, sizeY);
        }
    }
}

I’m still a beginner, so I’m wracking my brain to try and figure out how to add to my selections if in the selection box. This selection box seems a lot better than the other one though haha.

So, I imagine I could just move this,

        if (Input.GetMouseButtonUp(0))
        {
            if (isDragging)
            {
                DeselectBimbus();
                foreach (var selectableObject in FindObjectsOfType<BoxCollider>())
                {
                    if (IsInSelectBox(selectableObject.transform))
                    {
                        SelectBimbus(selectableObject.transform, true);
                        print(selectedBimbi);
                    }
                }
              
            }
            isDragging = false;
        }

To the selection box script, but how would I change it to make it add the objects to my list in the selection script? Can I use the centre variable somehow?

this is what I use for my RTS
I have not worked on it for a while. but you maybe able to salvage something from it.

This Script allows use to single unit select, Multiple unit select with drag. it also allows you to group them into group 1-9 and recall them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UnitSelectorDynamic : MonoBehaviour
{
    //------------- This Script only Selects Units that are UnSelected And Are Listen in "unSelectedUnitList"----------------

    public showOrHideHealthBars healthbarSwitchReference;

    public RectTransform selectorImage;

    private HealthBarRotation healthBarRotationReference;

    Rect selectionRect;
    Vector2 startPos;
    Vector2 endPos;

    Vector2 CameraBs;

   

    Rect fullScreenRect;
    float screenWidth = Screen.width;
    float screenHight = Screen.height;


    int count = 1;                             //This First Value is not really that Important. the lower the better, but I left it at 1 just incase - This is Used for saving

    void Start()
    {    


        startPos = Vector2.zero;       //Used to Calculate my drag Distance
        endPos = Vector2.zero;         //Used to Calculate my mouse drag distance
              

        DrawRectangle();       
    }

   
    void Update()
    {
        if(Input.GetMouseButtonDown(0))             //The instant the Left mouse boton is inititaly pressed     
        {

            float aDistance = screenWidth / 2;
            float bDistance = screenHight / 2;

            startPos = Input.mousePosition;

            selectionRect = new Rect();            // A reference for Draging Rectangle
            fullScreenRect = new Rect();           // A reference for Rectangle representing full Screen -- This is Used because every Screen Will be of different Size

           

         
               fullScreenRect.xMin = Camera.main.transform.position.x - aDistance;
               fullScreenRect.xMax = Camera.main.transform.position.x + aDistance;

               fullScreenRect.yMin = Camera.main.transform.position.z - bDistance;
               fullScreenRect.yMax = Camera.main.transform.position.z + aDistance;
               fullScreenRect.x = Camera.main.transform.position.x;                       // This is used because for some reason I currently Dont Understand "fullScreenRect.x or .y always comes up as negative.
               fullScreenRect.y = Camera.main.transform.position.y;                       // This is used because for some reason I currently Dont Understand "fullScreenRect.x or .y always comes up as negative.





        }
       
        if (Input.GetMouseButton(0))              // When Left Mouse button is Heled-Down                                          
        {
            endPos = Input.mousePosition;

            DrawRectangle();                     // Runs the Function to Draw the visual Rectangle that represents the real selection aread



            if (Input.mousePosition.x < startPos.x)
            {
                selectionRect.xMin = Input.mousePosition.x;  
                selectionRect.xMax = startPos.x;            
            }
            else
            {
                selectionRect.xMin = startPos.x;
                selectionRect.xMax = Input.mousePosition.x;
            }

            if (Input.mousePosition.y < startPos.y)
            {
                selectionRect.yMin = Input.mousePosition.y;
                selectionRect.yMax = startPos.y;
            }
            else
            {
                selectionRect.yMin = startPos.y;
                selectionRect.yMax = Input.mousePosition.y;
            }

           
        }         //Function to draw the Actual selection rect

        if (Input.GetMouseButtonUp(0))          //The instant the Left mouse botton is inititaly released
        {
            CheckAndSelectUnSelectedUnits();    //This calls our Main function, mouse selection and drag selection

            startPos = Vector2.zero;            //This Makes it so the rectangle DisApears after we Let go
            endPos = Vector2.zero;              //This Makes it so the rectangle DisApears after we Let go

            DrawRectangle();                    //**Normally:Runs the Function to Draw the visual Rectangle that represents the real selection aread, However, I placed this under "Input.GetMouseButtonUp" because in essence this acts as a Single mouse click which we use for "if (dist < 45)" calculations for single target
                                                //** ^ this top Function Can be changed to ray casting as we do not need it anymore, But I will keep it as a Learning Point
        }

        //----------------Saving Units into Groups--------------------

        if (Input.GetKey("left ctrl"))         // This is to Prevent alll these calls from being called every frame without reason Unless I "left ctrl"
        {                      
            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha1))   // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList1A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha2))   // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList2A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha3))   // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList3A();
            }
       
            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha4))  // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList4A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha5))  // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList5A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha6))  // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList6A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha7))  // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList7A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha8))  // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList8A();
            }

            if (Input.GetKey("left ctrl") && Input.GetKeyDown(KeyCode.Alpha9))  // Get Key Down, That way it will save this in the instant we Click it, Otherwise, it will keep saving it for everyframe that we have "the Number key" Heled Down
            {
                SaveToList9A();
            }
        }

       


        //----------------Calling Units From Their Groups------------------
        else                                                                   //the "else Statements are nessesary otherwise these functions will be called even when we are only trying to Save units into List as we also Pres the Number Buttons   
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {

            LoadList1();
            //Debug.Log("Recall:1");

        }                               //Cant Just use one else function (or maybe I can), not sure which uses less memory

        else
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            LoadList2();
            //Debug.Log("Recall:2");
        }

        else
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            LoadList3();
            //Debug.Log("Recall:3");
        }

        else
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            LoadList4();
            //Debug.Log("Recall:4");
        }

        else
        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            LoadList5();
            //Debug.Log("Recall:5");
        }

        else
        if ( Input.GetKeyDown(KeyCode.Alpha6))
        {
            LoadList6();
            //Debug.Log("Recall:6");
        }

        else
        if (Input.GetKeyDown(KeyCode.Alpha7))
        {
            LoadList7();
            //Debug.Log("Recall:7");
        }

        else
        if (Input.GetKeyDown(KeyCode.Alpha8))
        {
            LoadList8();
            //Debug.Log("Recall:8");
        }

        else
        if (Input.GetKeyDown(KeyCode.Alpha9))
        {
            LoadList9();
            //Debug.Log("Recall:9");
        }
    }

    void DrawRectangle()                       //Function to Draw the visual Rectangle that represents the real selection aread
    {
       
        Vector2 boxCenter = (startPos + endPos) / 2;

        selectorImage.position = boxCenter;    //Image evenely spreads it-self from the ceneter, this can be changed from its object settings                    
        float sizeX = Mathf.Abs(startPos.x - endPos.x);       //"Abs" returns +ve Values, cant draw Negative Distance but you can calculate it if you drag from postive to negative             
        float sizeY = Mathf.Abs(startPos.y - endPos.y);       //"Abs" returns +ve Values, cant draw Negative Distance but you can calculate it if you drag from postive to negative    

        selectorImage.sizeDelta = new Vector2(sizeX, sizeY);  // I wounder why we didnt use this to calculate the actual Selection area "selectionRect"
    }

    void CheckAndSelectUnSelectedUnits()                                     
    {


        foreach (Unit myUnit in SelectionManager.unSelectedUnitList)   // Everything Under here calls the units Placed within this list "unSelectedUnitList"         
        {
            float dist = Vector2.Distance(Input.mousePosition, (Camera.main.WorldToScreenPoint(myUnit.transform.position)));  // This calculates the x and y of LeftMouse Button Click "this was described above - This is needed for "if (dist < 45) calculations"

            RaycastHit hit;                                            // This returns the information from what the ray collides with



            if (Input.GetKey("left shift"))
            {


                if (selectionRect.Contains(Camera.main.WorldToScreenPoint(myUnit.transform.position)) && !myUnit.selected)    //"selectionRect.Contains" is everything within the area of the rectangle that we draw
                {                                                                                                                       //"Camera.main.WorldToScreenPoint" Everything the Main.Camera can see "check its Tag"
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        myUnit.SetSelector(true);                               //All units that satisfy the "if" function Logic will be turn on their "Visual Selection Circle"
                        myUnit.selected = true;                                //The tag of these Selected units will change to "Selected"
                        SelectionManager.SelectedUnitList.Add(myUnit);          //This is the real Deal, Now they are added to the Selected Units List. This way we can Command them such as /Move them/Force them to attack.. etc

                                              
                    }
                }
                else  //****!!!! If you Remove the "else", the next "if" Statemnt will always run!!!******   its  somthing that I always forget

                if ((dist < myUnit.sizeModefier) && !myUnit.selected)                //For Single Target Selection - Keep in mind this is with "left shift" also Pressed down, meaning it adds to selection                             
                {
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        myUnit.SetSelector(true);
                        myUnit.selected = true;
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                }
                else

                if ((dist < myUnit.sizeModefier) && myUnit.selected )                  //This is to Deselect Individual Units From a group that Have Already been Selected
                {

                    myUnit.SetSelector(false);                              //Removes the Visual Representation "Selection Circle"
                    myUnit.selected = false;                            //Changes tag to "Unselected" THIS IS IMPORTANT, because curently we can only Select Units that are initialy tagges as Unselected
                    SelectionManager.SelectedUnitList.Remove(myUnit);       //Most Importantly, we Remove them from the selected List, therefore we dont command them when they are un-Sleected
                }
                else

                //========================================THIS IS FOR ctrl + SELECTING ALL UNITS WITH SAME NAME ON THE SCREEN===========================================================
                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity) && (hit.collider.transform.tag == "Player"))   // This Raycasting, will return true when used against both Selected and Unselected Units. The purpose of this is to get the name of the unit "hit" by the ray //Changed from "&& (hit.collider.transform.tag == "UnSelected" || hit.collider.transform.tag == "Selected")"
                {
                    string nameOfTarget = hit.collider.transform.name;      // tried to use it for the Buttom Function be didnt work so I used "hit.collider.transform.name" instead

                    if (Input.GetKey("left ctrl")) //**Should probabaly replace this "if Statemnt location with the Ray Castone, Will probabaly be more effeciant", probabaly much more effeciant (we do use shift so its not that bad.     // Dont forget, this function requires you to press "left shift" + "left ctrl" look above for reasoning
                    {

                        if (fullScreenRect.Contains(Camera.main.WorldToScreenPoint(myUnit.transform.position)) && (!myUnit.selected || myUnit.selected) && myUnit.name == nameOfTarget)   // "fullScreenRect.Contains" is our screen Size, Basically a rectangle the size of our screen which gets drawn as we Ctrl + left Moust click!
                        {                                                                                                                                                                                                   //"Camera.main.WorldToScreenPoint" Everything the Main.Camera can see that is overlapped by "fullScreenRect.Contains" will be selected. (which is everything)
                            if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                            {
                                myUnit.SetSelector(true);                                                                                                                                                                       //*** myUnit.name == hit.collider.transform.name *** everything within our screen due to the 2 lines above^ , with the same name as the "hit" by ray target will be selected
                                myUnit.selected = true;
                                SelectionManager.SelectedUnitList.Add(myUnit);
                                // Debug.Log("Did Hit" + nameOfTarget);
                            }
                        }

                    }
                }
                //=======================================================================================================================================================================
            }
            else

            if (selectionRect.Contains(Camera.main.WorldToScreenPoint(myUnit.transform.position)) && !myUnit.selected)
            {
                if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    SelectionManager.SelectedUnitList.Add(myUnit);
                }
                   

            }
            else

            if (dist < myUnit.sizeModefier)            //In future, I can change these to RayCasting
            {
                // Debug.Log("Full" + fullScreenRect);
                // Debug.Log("Selection" +  selectionRect);
                // Debug.Log("Selection" + selectionRect);

                if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    SelectionManager.SelectedUnitList.Add(myUnit);
                }
                   
            }
            else

            if (Input.GetKey("left ctrl"))   //IMPORTANT Place the "Input.GetKey("left ctrl)" before the rest of the logical "if" function in this paragraph. otherwise it will always hold true and not proceed to "if (myUnit.tag == "Selected")"
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;
                SelectionManager.SelectedUnitList.Remove(myUnit);

                if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity) && (hit.collider.transform.tag == "Player"))
                {
                    string nameOfTarget = hit.collider.transform.name;
                   
                    if (fullScreenRect.Contains(Camera.main.WorldToScreenPoint(myUnit.transform.position)) && (!myUnit.selected || myUnit.selected) && myUnit.name == hit.collider.transform.name)
                    {
                        if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                        {
                            myUnit.SetSelector(true);
                            myUnit.selected = true;
                            SelectionManager.SelectedUnitList.Add(myUnit);
                            //  Debug.Log("Did Hit" + nameOfTarget);
                        }

                    }

                   
                }
            }           
            else //----------------------THIS REMOVES ALL UNITS (Even if there are dupes of the same Unit) because there is no Breaks and theres no need for break due to the use of "else" statemnt everytime, Meaning we are never adding and removing at the same time-----
            //Due to no Curcly bracers, this else statemnt only runs the next "if" statemnt and not the one to make units visibile
            if (myUnit.selected)                            //This basically Deselects Everything that was selected if you click on space 
            {
                myUnit.SetSelector(false);                           //Turns off and Removes the Visual "Selection Circle Indicator" of all units
                myUnit.selected = false;                          //Turns the tag of everything that was Selected to "Unselected"
                SelectionManager.SelectedUnitList.Remove(myUnit);    //Removes and Basically Purges the "SelectedUnitList"

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();   //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                              //This makes all its children Layer change to invisibile
                    }
                }               

            }
       
            //===================THIS MAKES UNITS VISIBLE IF SELECTED=============
            //This will run Indipendant from the code above.
            if(myUnit.selected)
            {
                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }                         

            }
          
        }

    }

    //---------------------------Saving Number Lists----------------------------------

    //===============================Note==========================


/*
    void SaveToList1A()
    {
        bool cleared = false;
        Debug.Log(SelectionManager.SelectedUnitList.Count);
        for (int i = 0; i < SelectionManager.SelectedUnitList.Count; i++)
        {
            if (Input.GetKey(KeyCode.LeftShift))
            {
                if (!SelectionManager.ctrlGroup1.Contains(SelectionManager.SelectedUnitList[i]))
                {
                    SelectionManager.ctrlGroup1.Add(SelectionManager.SelectedUnitList[i]);
                }
            }

            else
            {
                if (!cleared)
                {
                    SelectionManager.ctrlGroup1.Clear();
                    cleared = true;
                }
                SelectionManager.ctrlGroup1.Add(SelectionManager.SelectedUnitList[i]);
            }
        }

    }

*/










    void SaveToList1A()
    {
        count = 0;

        if (Input.GetKey("left shift"))  //I may Need to remove this all together?? this part here doesnt really make sense
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup1.Contains(myUnit))    //to prevent adding Duplicate units (of the same unit) to the code, this Happens when you try to add more units while selected units that are already in your group
                {
                    SelectionManager.ctrlGroup1.Add(myUnit);          //Adding all units that are not in my list
                }               
                count = SelectionManager.ctrlGroup1.Count;         

            }
        }
        else
        {
           
            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup1)
            {            
                if (!myUnit.selected)
                {                   
                    count += 1;                 
                }               
            }
            //-----------------------------------------------------------------------------------------------------------
           
            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
               // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {                  
                    SaveToList1B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }               
            }
            //-------------------------------------------------------------------------------------------------------------
           
            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {              
                    if (!SelectionManager.ctrlGroup1.Contains(myUnit))     //to prevent adding Duplicate units (of the same unit) to the code, this Happens when you try to add more units while selected units that are already in your group
                    {
                        SelectionManager.ctrlGroup1.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList1B()
    { 
        foreach (Unit myUnit in SelectionManager.ctrlGroup1)
        {
          
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {               
                SelectionManager.ctrlGroup1.Remove(myUnit);               
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;  // Once you remove one unit from list, break and exit from the loop, if I dont break, it will break due to iteration (stupid bs)
        }
    }




    void SaveToList2A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup2.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup2.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup2.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup2)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
               
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup2.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup2.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList2B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup2)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup2.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }





    void SaveToList3A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup3.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup3.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup3.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup3)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
               
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList3B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup3.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup3.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList3B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup3)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup3.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }





    void SaveToList4A()
    {

        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup4.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup4.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup4.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup4)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
               
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList4B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup4.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup4.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList4B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup4)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup4.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }





    void SaveToList5A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup5.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup5.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup5.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup5)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
               
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                //Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList5B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup5.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup5.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList5B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup5)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup5.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }






    void SaveToList6A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup6.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup6.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup6.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup6)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
               
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList6B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup6.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup6.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList6B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup6)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup6.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }





    void SaveToList7A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup7.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup7.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup7.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup7)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
              
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList7B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup7.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup7.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList7B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup7)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup7.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }





    void SaveToList8A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup8.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup8.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup8.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup8)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
              
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList8B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup8.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup8.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList8B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup8)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup8.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }






    void SaveToList9A()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (!SelectionManager.ctrlGroup9.Contains(myUnit))
                {
                    SelectionManager.ctrlGroup9.Add(myUnit);
                }
                count = SelectionManager.ctrlGroup9.Count;
            }
        }
        else
        {

            //------------------Check How Many times to run the Next Function---------------------
            foreach (Unit myUnit in SelectionManager.ctrlGroup9)
            {

                if (!myUnit.selected)
                {
                    count += 1;
                    //Debug.Log("First count ammount saved" + " " + count);
                }
              
            }
            //-----------------------------------------------------------------------------------------------------------

            //Run the function (which Calls onto a different function) based on the number given above----------------------some sort of loop
            if (count > 0)
            {
                // Debug.Log("Second count test ammount saved" + " " + count);
                for (int i = 0; i < count; i++)
                {
                    SaveToList9B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }
            //-------------------------------------------------------------------------------------------------------------

            //------------------------------Run this at the end-------------------------------------------------------
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                if (myUnit.selected)
                {
                    if (!SelectionManager.ctrlGroup9.Contains(myUnit))
                    {
                        SelectionManager.ctrlGroup9.Add(myUnit);
                    }
                }
            }
            //------------------------------------------------------------------------------------------------------  
        }
    }

    void SaveToList9B()
    {
        foreach (Unit myUnit in SelectionManager.ctrlGroup9)
        {
            if (!myUnit.selected)  // This will never be "UnSelected because all my units in this list are selectes"           
            {
                //int count = SelectionManager.ctrlGroup1.Count;
                SelectionManager.ctrlGroup9.Remove(myUnit);
                //Debug.Log("Removed");
                //Debug.Log("Third count test ammount saved" + " " + count);
            }
            else // else if my unit is selected
            {
                continue;   // this ensue we continue if the first unit we encounter isnt a unit that we already have selected (otherwise it would break without removing a single unit)
            }
            break;
        }
    }
           
          
    //---------------------------Loading Number Lists----------------------------------

    void LoadList1()
    {
        count = 0;

        if (Input.GetKey("left shift"))  // I have a problem with shift... WHY????
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup1)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))     //To prevent the same Unit getting added sevral times to a list, due to it already being selected. it may not be visible, but it takes space and memory!
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);                       
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }               
            }
        }
        else
        {   //Using curly Bracks with this (else).. Very Important so nother else runs while shift is pressed     
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;    //**** Lol I dont need a foreach here.. I should just do if (SelectionManager.SelectedUnitList.Count>0) in the next function!
                //count = SelectionManager.SelectedUnitList.Count; //This is wrong, Copy the method I used for saving // this Might still work as a result of over kill, This needs fixing
            }       

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList1B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup1)
            {                     
                 myUnit.SetSelector(true);
                 SelectionManager.SelectedUnitList.Add(myUnit);
                 myUnit.selected = true;
            }
        }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }      
    }

    void LoadList1B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }               

                SelectionManager.SelectedUnitList.Remove(myUnit);


                break; // Does this not mean that its only getting applied to one unit?
            }
        }
    }



    void LoadList2()
    {
        count = 0;

          if (Input.GetKey("left shift"))
          {
              foreach (Unit myUnit in SelectionManager.ctrlGroup2)
              {
                  if(!myUnit.selected)
                  {
                      myUnit.SetSelector(true);
                      myUnit.selected = true;                    
                     if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                     {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                     }
                     count = SelectionManager.SelectedUnitList.Count;


                  }
                 
              }
          }
          else
          {        
              foreach (Unit myUnit in SelectionManager.SelectedUnitList)
              {
                    count += 1;
              }

              if (count > 0)
              {
                  for (int i = 0; i < count; i++)
                  {
                      LoadList2B();
                      //Debug.Log("Second count test ammount saved" + " " + count);
                  }
              }

              foreach (Unit myUnit in SelectionManager.ctrlGroup2)
              {
                  myUnit.SetSelector(true);
                  SelectionManager.SelectedUnitList.Add(myUnit);
                  myUnit.selected = true;
              }
          }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }

    }

    void LoadList2B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {          
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList3()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup3)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup3)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }


        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }
    }

    void LoadList3B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList4()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup4)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup4)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }
    }

    void LoadList4B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList5()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup5)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup5)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }
    }

    void LoadList5B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList6()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup6)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup6)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }

    }

    void LoadList6B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList7()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup7)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup7)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }
    }

    void LoadList7B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList8()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup8)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup8)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }


        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }
    }

    void LoadList8B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }




    void LoadList9()
    {
        count = 0;

        if (Input.GetKey("left shift"))
        {
            foreach (Unit myUnit in SelectionManager.ctrlGroup9)
            {
                if (!myUnit.selected)
                {
                    myUnit.SetSelector(true);
                    myUnit.selected = true;
                    if (!SelectionManager.SelectedUnitList.Contains(myUnit))
                    {
                        SelectionManager.SelectedUnitList.Add(myUnit);
                    }
                    count = SelectionManager.SelectedUnitList.Count;
                }

            }
        }
        else
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)
            {
                count += 1;
            }

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    LoadList2B();
                    //Debug.Log("Second count test ammount saved" + " " + count);
                }
            }

            foreach (Unit myUnit in SelectionManager.ctrlGroup9)
            {
                myUnit.SetSelector(true);
                SelectionManager.SelectedUnitList.Add(myUnit);
                myUnit.selected = true;
            }
        }

        if (healthbarSwitchReference.allyHealthbars == true)
        {
            foreach (Unit myUnit in SelectionManager.SelectedUnitList)                                             //Iterating to Activate Health Bars
            {
                if (myUnit.selected)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 5;                                                                //This makes all its children Layer change to Visibile
                    }
                }

            }
        }
    }

    void LoadList9B()
    {
        foreach (Unit myUnit in SelectionManager.SelectedUnitList)
        {
            {
                myUnit.SetSelector(false);
                myUnit.selected = false;

                if (healthbarSwitchReference.allyHealthbars == true)
                {
                    HealthBarRotation tempSave = myUnit.gameObject.GetComponentInChildren<HealthBarRotation>();    //getting refrence for healthBar UI

                    foreach (Transform child in tempSave.transform)
                    {
                        child.gameObject.layer = 19;                                                                //This makes all its children Layer change to inVisibile
                    }
                }

                SelectionManager.SelectedUnitList.Remove(myUnit);
                break;
            }
        }
    }  

}