Want Btn made from prefab in script to call a function in a script.

m trying to make a control for mobile to click a button that gets created when you select a unit. all this works so far. once clicked it should call a mousemode change to movemode. this then waits for the player to click a tile and feeds the tile to the unit so it knows where to go. Perhaps im taking a dumb way about implementing this and there is a better way but i shall go through my code.

So here is the play area. when a tile is selected.

When the unit is selected it makes a button and runs this code to it to try to call move function.

    private void GetInterfaceButtonField(string name, UIField uIField, ButtonFunc func)
    {

        GameObject buttonBox = Instantiate(buttonPrefab, new Vector3(0, 0, 0), Quaternion.identity);
        RectTransform trans = buttonBox.GetComponent<RectTransform>();

        trans.sizeDelta = new Vector2(100, 30);
        trans.pivot = new Vector2(0, 1); //top left
       
        Text text = buttonBox.GetComponentInChildren<Text>();
        text.text = name;
        text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font; //getting basic font
        text.fontStyle = FontStyle.Normal; //not really needed, auto dose this
        text.fontSize = 14; //standard 
        text.color = new Color(0, 0, 0); //Black

        Button btn = buttonBox.GetComponent<Button>();

        if (func == ButtonFunc.MOVE)
        {
            Debug.Log("Set Move Fun");
            btn.onClick.AddListener(() => MoveBtnFunc());
        } 
        else if(func == ButtonFunc.ATTACK)
        {
            btn.onClick.AddListener(() => AttackBtnFunc());
        }



        buttonBox.layer = 5;
        buttonBox.transform.SetParent(GetParent(uIField).transform, false);

        textObjects.Add(name, buttonBox);
    }

private void MoveBtnFunc()
    {

        MouseController mCtrl = FindObjectOfType<MouseController>();
        mCtrl.MoveMode();
        Debug.Log("MoveBtnFunc");
    }


and the debug shows me that works.

now when i click the button to go in to move mode it should change the mouse controller to move mode that skips out selection mode script as we are not in that mode and runs move mode script. how ever i cant seem to get it to change the mouse mode.

heres more code and the img showing the debugs coming through

MouseController

    void Update ()
    {

        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hitInfo, 200))
        {
            currFramePosition = hitInfo.point;
            currFramePosition.y = 0;
            //Debug.DrawRay(Camera.main.transform.position ,hitInfo.point, Color.red, 1f);
            UpdateCursor();

            //UpdateDragging();
            UpdateCameraMovement();
            UpdateSelection();
            UpdateMoveMode();

        }

        if( Input.GetKeyUp(KeyCode.Escape) ) {
            if(currentMode == MouseMode.BUILD) {
                currentMode = MouseMode.SELECT;
            }
            else if( currentMode == MouseMode.SELECT ) {
                Debug.Log("Show game menu?");
            }
            intController.SetFieldString("MouseMode: " + currentMode.ToString(), "MouseMode");
        }
        // Save the mouse position from this frame
        // We don't use currFramePosition because we may have moved the camera.
        lastFramePosition = hitInfo.point;
        lastFramePosition.y = 0;
    }


    void UpdateSelection()
    {
        // This handles us left-clicking on structures or units to set a selection.

        if (Input.GetKeyUp(KeyCode.Escape))
        {
            mySelection = null;
            intController.CleanUp();
            //destroy text fields
            //if (fields)
            //{
            //    Destroy(fields);
            //}
        }

        if (currentMode != MouseMode.SELECT)
        {
            return;
        }

        // If we're over a UI element, then bail out from this.
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }
//...... blah blah blah
}


    void UpdateMoveMode()
    {
        if (currentMode != MouseMode.MOVE)
        {
            return; //not in move mode. escape
        }

       

        if (Input.GetMouseButtonDown(0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            Tile t = GetMouseOverTile(); //get curr frame tile

            ISelectableInterface actualSelection = mySelection.stuffInTile[mySelection.subSelection];

            if (actualSelection.IsUnit() == true)
            {
                Debug.Log("Setting units destination to Tile: " + t.X + "," + t.Z);
                mySelection.tile.unit.SetDestination(t); //set destination for the unit to go to from the 
                                                                             //current frame tile that the mouse is over
            }
            else
            {
                Debug.LogError("Move was somehow called on " + actualSelection.ToString());
            }
        }
        currentMode = MouseMode.SELECT;
    }


    public void MoveMode()
    {
        currentMode = MouseMode.MOVE;
        //yield return new WaitUntil(() => Input.GetMouseButtonUp(0) == true);
    }

as you can see the debug from the move btn is being called.

i know its not an exciting issue but i could really use an idea on how to work around this problem#