Unity UI Button OnClick() Not Working. Normal solutions havent solved

Hey! I am currently making a turn based game, and i have noticed that the ui of my end turn button wont activate the function in my script to change turns. I have tried remaking buttons, remaking the canvas, adding debug.logs and all i have found is that the function isnt being called at all. I have selected the function in the button, i have a canvas with a raycast thing, i have the event system so i dont know what is wrong.


1 Like
  1. Is there any other UI above the button in terms of layering that blocks raycasts for MainUI canvas? e.g. UnitSelectionBox - what is it?
  2. What’s going on near FirstTurn message? Please, provide a code

Thanks for responding! So there is no other UI that is blocking it, also the colours change when i hover / click the button. Here is the code for my Turn Controller, and the debug in the console is just that in start the First Turn is called(there is also a second turn, but that is basically the same but switches values):

private void Start()
    {
        FirstPlayerTurn();
    }


    public void FirstPlayerTurn()
    {
        Debug.Log("FirstTurn");
        secondCam.gameObject.SetActive(false);
        firstCam.gameObject.SetActive(true);
        if (UnitSelectionBox.Instance != null) UnitSelectionBox.Instance.myCam = firstCam;
        UnitSelectionManager.Instance.cam = firstCam;
        buttonOne.SetActive(true);
        buttonTwo.SetActive(false);
        if (chosenFactionOne == Faction.Stargazer)
        {
            factionOneUnits = GameObject.FindGameObjectsWithTag("Stargazer");

        }
        else if (chosenFactionOne == Faction.Flaming)
        {
            factionOneUnits = GameObject.FindGameObjectsWithTag("Flaming");
        }

        if (chosenFactionTwo == Faction.Stargazer)
        {
            factionTwoUnits = GameObject.FindGameObjectsWithTag("Stargazer");
        }
        else if (chosenFactionTwo == Faction.Flaming)
        {
            factionTwoUnits = GameObject.FindGameObjectsWithTag("Flaming");
        }
        foreach (GameObject unit in factionOneUnits)
        {
            unit.layer = 0;
            unit.layer = 7;
            unit.GetComponent<AttackController>().isPlayer = true;
            unit.GetComponent<UnitMovement>().cam = firstCam;
        }

        foreach(GameObject unit in factionTwoUnits)
        {
            unit.layer = 0;
            unit.layer = 8;
            unit.GetComponent<AttackController>().isPlayer = false;
        }
    }`

Are the buttons on the same place?
Try to move one of them to not cover the other one: doesn’t matter that it’s deactivated.

For testing purposes i have had them not cover eachother. Even then the button wont call the function

1 Like

Is TurnController active when you click?

Also, make a clear experiment:

  1. Create 1 more canvas from scratch in hierarchy (do not duplicate).
  2. Create a button and assign target function.
  3. Test.

Yes, turn controller is active, and i have made a new canvas and yet it still wont work ;-;

1 Like

Did you try to use different order in layer of canvases?
Can You share demo scene somehow?

Nevermind, i have found the problem. The selection box blocks it for some reason, and i have fixed the issue. It is its own canvas (which i forgot about xD), so do i just removed the canvas raycast on it and all was fixed, thanks for helping anyways though : D

So I supposed this above, you had to recheck it at that moment