Search Unity

Buttons frozen in pressed state when changing menu page

Discussion in 'UGUI & TextMesh Pro' started by Tidvis_Martin, Apr 21, 2019.

  1. Tidvis_Martin

    Tidvis_Martin

    Joined:
    Mar 12, 2019
    Posts:
    24
    Hello, unity forum!
    A few weeks back I started work on our menus and menu systems. The script I made for changing the menu was quite simple, put everything from one page under an empty parent object, and when a button is pressed to change the page, disable the parent and enable the parent of the page I want to switch to. Easy, right? Apparently not so, the issue I came across was that when I did this the buttons would end up getting stuck in their highlighted state (note that my buttons are animated buttons using an animator controller, this does not seem to be the case with any other type of button). Unity, when queried, would degub.log that the button was in its normal state even though it was displaying the highlighted/pressed state it had been in when the page had been disabled. No matter what I tried, playing another clip, disabling and reenabling the button, setting it to another state and back again, making it un-interactable and interactable again, it was stuck until I turned off play mode and back on again.

    Now, this is a post for posterities sake, I managed to solve this issue.
    Simply, instead of using SetActive() to disable and reenable a parent game object for all the menu elements I am moving the parent game object 10000 units down (but any directions works).

    Heres my code, very simple, some might even say crude. If anyone has any other suggestions I am more than open to hearing about it, but for now this works for me and I simply wanted to post about this and how I solved it in case anyone else has the same issue now or in the future, have a great day!

    Code (CSharp):
    1.     public static void SwitchToPage(GameObject menupage)
    2.     {
    3.         RectTransform rect = menupage.GetComponent<RectTransform>();
    4.  
    5.         if (rect != null)
    6.         {
    7.             rect.SetBottom(0);
    8.             rect.SetTop(0);
    9.  
    10.             menupage.transform.SetAsLastSibling();
    11.         }
    12.         else
    13.         {
    14.             menupage.transform.localPosition = Vector3.zero;
    15.             menupage.transform.SetAsLastSibling();
    16.         }
    17.     }
    18.  
    19.     public static void SwitchFromPage(GameObject menupage)
    20.     {
    21.         RectTransform rect = menupage.GetComponent<RectTransform>();
    22.  
    23.         if (rect != null)
    24.         {
    25.             rect.SetBottom(10000);
    26.             rect.SetTop(-10000);
    27.  
    28.             menupage.transform.SetAsFirstSibling();
    29.         }
    30.         else
    31.         {
    32.             menupage.transform.localPosition = new Vector3(0, 10000);
    33.             menupage.transform.SetAsFirstSibling();
    34.         }
    35.     }