Search Unity

Unity UI UI buttons on a panel not working

Discussion in 'UGUI & TextMesh Pro' started by zcoursey, May 22, 2017.

  1. zcoursey

    zcoursey

    Joined:
    May 18, 2017
    Posts:
    16
    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.

    Code (CSharp):
    1. public GameObject chao;
    2.  
    3.     public GameObject chaoPanel;
    4.     public GameObject[] chaoList;
    5.  
    6. void Start()
    7.     {
    8.         if (!PlayerPrefs.HasKey("looks"))
    9.         {
    10.             PlayerPrefs.SetInt("looks", 0);
    11.             createChao(PlayerPrefs.GetInt("looks"));
    12.         }
    13.     }
    14.  
    15. public void buttonBehavior (int i)
    16.     {
    17.         switch(i)
    18.         {
    19.             case (0):
    20.             default:
    21.                 chaoPanel.SetActive(!chaoPanel.activeInHierarchy);
    22.                 break;
    23.             case (1):
    24.  
    25.                 break;
    26.             case (2):
    27.  
    28.                 break;
    29.  
    30.             case (3):
    31.                 if (MusicPlayer.GetComponent<AudioSource>().isPlaying)
    32.                 {
    33.                     MusicPlayer.GetComponent<AudioSource>().Pause();
    34.                 }
    35.                 else
    36.                 {
    37.                     MusicPlayer.GetComponent<AudioSource>().UnPause();
    38.                 }
    39.                 break;
    40.             case (4):
    41.                 chao.GetComponent<Chao>().saveChao();
    42.                 Application.Quit();
    43.                 break;
    44.         }
    45.     }
    46.  
    47.     public void createChao (int i)
    48.     {
    49.         if (chao)
    50.         {
    51.             Destroy(chao);
    52.             chao = Instantiate(chaoList[i], new Vector3(0,0,-2), Quaternion.identity) as GameObject;
    53.         }
    54.         if (chaoPanel.activeInHierarchy)
    55.             chaoPanel.SetActive(false);
    56.  
    57.         PlayerPrefs.SetInt("looks", i);
    58.     }
     
  2. zcoursey

    zcoursey

    Joined:
    May 18, 2017
    Posts:
    16
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,689
    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.
     
  4. pas0003

    pas0003

    Joined:
    Mar 24, 2013
    Posts:
    18
    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 ()?
     
  5. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    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)
     
    ambahk likes this.
  6. ig1r

    ig1r

    Joined:
    Nov 12, 2018
    Posts:
    2
    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

    Code (CSharp):
    1.  
    2.  
    3. MainMenuPanel.SetActive(true);
    4. LoadingPanel.SetActive(false);
    5. InteractivePanel.SetActive(false);
    6.  
    7.  
    8. Vector3 goAway = new Vector3(-10000, -10000, -10000);
    9.  
    10. RectTransform myRectTransform = LoadingPanel.GetComponent<RectTransform>();
    11. myRectTransform.localPosition = goAway;
    12.  
    13. RectTransform myRectTransform2 = InteractivePanel.GetComponent<RectTransform>();
    14. 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
     
  7. Mo0p

    Mo0p

    Joined:
    Jul 15, 2014
    Posts:
    1
    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.
     
  8. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    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.

    Screenshot 2019-05-30 17.26.35.png
     
    ig1r likes this.
  9. AbdulrahmanKarim

    AbdulrahmanKarim

    Joined:
    Jun 12, 2018
    Posts:
    1
    Be sure the Raycast Target is unchecked in the Panel.
     
    MarkusHenrich likes this.
  10. AbleeC

    AbleeC

    Joined:
    Feb 7, 2023
    Posts:
    1
    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.