Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Possible bug in OnClick()

Discussion in 'UGUI & TextMesh Pro' started by egbokalaka, Jul 15, 2023.

  1. egbokalaka


    Jun 12, 2023
    I have a group of controls that I want to present in a mutually exclusive fashion. These are controlled by three buttons in the UI, with OnClick handlers pointing to separate routines in my code:

    Code (CSharp):
    1.  public void HelpButtonPressed()
    2.     {
    3.         Debug.Log("Help button pressed");
    4.         privacyView.SetActive(false);
    5.         creditsView.SetActive(false);
    6.         bool isActive = helpScrollView.activeSelf;
    7.         helpScrollView.SetActive(!isActive);
    8.     }
    10.     public void CreditButtonPressed()
    11.     {
    12.         Debug.Log("Credit button pressed");
    13.         privacyView.SetActive(false);
    14.         helpScrollView.SetActive(false);
    15.         bool isActive = creditsView.activeSelf;
    16.         creditsView.SetActive(!isActive);
    17.     }
    18.     public void PrivacyButtonPressed()
    19.     {
    20.         Debug.Log("Privacy button pressed");
    21.         creditsView.SetActive(false);
    22.         helpScrollView.SetActive(false);
    23.         bool isActive = privacyView.activeSelf;
    24.         privacyView.SetActive(!isActive);
    25.     }
    Here are my handler setups for two of the buttons:


    When I alternately click on the privacy and credit buttons, it all works as expected. However, when I toggle the credit button off, then toggle the privacy button on, then hit the credit button again, sometimes the UI system fires the OnClick event for the privacy button when I press the credit button! It get's stuck in this mode until I perform a seemingly random (meaning I can't discern a pattern) series of toggles between the two buttons. Is this a known issue? Am I calling SetActive() too frequently?
  2. egbokalaka


    Jun 12, 2023
    i discovered that the clickable region of my privacy button overlapped half of my credits button, far exceeding the limits of its image. not knowing how to fix this directly, I simply offset the buttons vertically, which worked around the issue. I was under the impression that the clickable area of a button was supposed to be constrained to the boundary of its image. So I'm uncertain if this is a bug or not.
  3. egbokalaka


    Jun 12, 2023
    The problem was the width of the text field's rect transform. I was unaware that this affected the button's clickable region. Newbie error.