UI buttons on a panel not working

I can open up this panel and close it just fine with a separate button, but the four buttons on this panel are completely non-responsive. I have an EventSystem and I believe all the buttons are correctly linked to where they should be.

public GameObject chao;

    public GameObject chaoPanel;
    public GameObject[] chaoList;

void Start()
    {
        if (!PlayerPrefs.HasKey("looks"))
        {
            PlayerPrefs.SetInt("looks", 0);
            createChao(PlayerPrefs.GetInt("looks"));
        }
    }

public void buttonBehavior (int i)
    {
        switch(i)
        {
            case (0):
            default:
                chaoPanel.SetActive(!chaoPanel.activeInHierarchy);
                break;
            case (1):

                break;
            case (2):

                break;

            case (3):
                if (MusicPlayer.GetComponent<AudioSource>().isPlaying)
                {
                    MusicPlayer.GetComponent<AudioSource>().Pause();
                }
                else
                {
                    MusicPlayer.GetComponent<AudioSource>().UnPause();
                }
                break;
            case (4):
                chao.GetComponent<Chao>().saveChao();
                Application.Quit();
                break;
        }
    }

    public void createChao (int i)
    {
        if (chao)
        {
            Destroy(chao);
            chao = Instantiate(chaoList[i], new Vector3(0,0,-2), Quaternion.identity) as GameObject;
        }
        if (chaoPanel.activeInHierarchy)
            chaoPanel.SetActive(false);

        PlayerPrefs.SetInt("looks", i);
    }

Bump

Seems like an interesting case. Try simply using debug statements on the scripts that your buttons are wired to.

Also check if the button “looks” like it is being clicked (should change colour, if not change the highlighted options). If Raycasts are not getting to the panel, check that you don’t have a CanvasGroup or other control higher in the hierarchy blocking raycasts or interactivity.

Hope that helps.

Same issue here! 2 panels - same settings in both. Either is visible at a time based on the game state. Buttons in first panel work. Buttons in second panel do not. I’m using the SetActive to show and hide panels.

Confirmed with editor that there is no weird overlaps and panels are correctly hidden. Funnily enough disabling the panels manually in the editor at runtime makes buttons in both panels work as expected. …

Is this a bug in SetActive ()?

When this happen I usually try adding a new button while in Play Mode on the Canvas and click it. Does it work? (Is it getting darker when you click it.) If it works, you slowly move that button down the tree and click it again until it is directly near your unresponsive button. If somehow it stops working, you can start deducing problem why it does not work.

If a fresh button does not work outright after adding it on top of the Canvas you probably have raycasting problem or event system problem. (Which you said you already have the event system so it should not happen)

1 Like

I had same issue where a canvas has 3 panels but only one of them should be active at a time, so the second two were disabled using SetActive(false), after that the button from third panel was not working, it seems because one of those 2 disabled panels were catching the ray…

the solution were not only in disabling those 2 panels but also moving them away to leave active panel at position Vector2.zero to catch events

MainMenuPanel.SetActive(true);
LoadingPanel.SetActive(false);
InteractivePanel.SetActive(false);


Vector3 goAway = new Vector3(-10000, -10000, -10000);

RectTransform myRectTransform = LoadingPanel.GetComponent<RectTransform>();
myRectTransform.localPosition = goAway;

RectTransform myRectTransform2 = InteractivePanel.GetComponent<RectTransform>();
myRectTransform2.localPosition = goAway;

I know that it is a workaround but it works and at the moment im not very proficient in unity to find another more elegant solution

had the same problem.
It was simply setting the sorting layer of the Canvas object (it was the object I hid and activated as well)
that fixed the problem for me.

FYI you could use CanvasGroup’s interactable field and block raycast field on the top object of the panel to mass-control things. In my game, each button should have something that catch the event (like an image with Raycast Target) then when it is loading or transitioning away, I simply tell the top CanvasGroup to be uninteractable so player couldn’t double press the button on transition sequence. Setting alpha to 0 + unblock raycast is also acceptable alternative to set active = false, since maybe your OnEnable on the entire tree is costly.

1 Like

Be sure the Raycast Target is unchecked in the Panel.

1 Like

I also had this issue! I figured out that my problem was, that I had a TMP Text as a child of the panel, and that prevented me from interacting with any UI element for some reason. Edit: I realised that the problem was, that the text was covering all the buttons.

1 Like

Same problem, I added Graphic raycaster to the panel and now it works. Why? No idea.