void OnGUI()

    {

        //locate world position of the click

        Vector3 Point = new Vector3();

        Event   currentEvent = Event.current;

        Vector2 mousePos = new Vector2();

        mousePos.x = currentEvent.mousePosition.x;

        mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;

        Point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));

        if (Input.GetMouseButtonDown(0))

        {

            //rep is a variable that avoids the editor repeating this conditional 

            //because without it it just does it twice for some reason

            if (rep==false)

            {

            //raycast

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit2D raycastHit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity,playerLayer);

            //if it hits something

            if (raycastHit.collider!=null)

            {

                //currentNum is the index of the selected unit

                //isSet is a boolean list that indicates if a unit has been selected

                if (currentNum>isSet.Count-1)

                {

                    currentNum=playerUnits.IndexOf(raycastHit.transform.gameObject);

                    isSet[currentNum]=true;

                }

                //if the unit with index currentNum is selected

                if (isSet[currentNum])

                {

                isChasing[currentNum]=false;

                //targets is the coord it will move to

                targets[currentNum]=Point;

                //sfx

                movementOrder.Play();

                //coroutine for moving to that point

                StartCoroutine(move(Point, playerUnits[currentNum], currentNum));

                //unselects

                isSet[currentNum]=false;

                }

                //if its not it selects it

                else{

                currentNum=playerUnits.IndexOf(raycastHit.transform.gameObject);

                if (raycastHit.transform.gameObject.tag=="Player" && isSet[currentNum]==false)

                {

                    currentNum=playerUnits.IndexOf(raycastHit.transform.gameObject);

                    isSet[currentNum]=true;

                }

                }

            }

            //if it doesnt hit anything it just starts the move coroutine

            else

            {

                if (isSet[currentNum])

                {

                isChasing[currentNum]=false;

                targets[currentNum]=Point;

                movementOrder.Play();

                StartCoroutine(move(Point, playerUnits[currentNum], currentNum));

                isSet[currentNum]=false;

                }

            }

            //makes rep true so it doesnt repeat

            rep=true;

            }

            else

            {

                //if rep is true it makes it false

                rep=false;

            }

        }

    }