Author Topic: Strange error regarding buttons that are disabled  (Read 6000 times)

prinky

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 13
    • View Profile
Strange error regarding buttons that are disabled
« on: August 04, 2013, 12:22:28 AM »
This might be a bit complicated so it will take a bit of explanation.

I have two ui panels. One of them has a set of three buttons (panel A), each of which transition to another panel (call it panel B).  If the user clicks one of the buttons, panel A fades out and loads the following ui panel (which "fades in").

A fade out process basically activates the tween object of the panel, causing it to go transparent. At the end of the tween, a function is called which disables the uipanel's gameObject (using something like NGUITools.SetActive(gameObject, false)). Right as the fade begins, the buttons of the fading panel are disabled to prevent any spurious clicks during the fade procedure (by setting each button's isEnabled property to false).

Basically panel A is supposed to fade out while it's buttons are disabled, and it's supposed to be disabled after  fading completes. That works for the most part, except when it does the fading procedure the first time after the menu loads and the user clicks a button, the buttons can be clicked during the fade -- they are not disabled even though they are in the code. That's the bug. Now let's say the user goes back from panel B to panel A. The same button is clicked again and panel A fades again. This time the UI behaves perfectly.  So this bug only occurs once...and I'm not sure why.

Anyway, I can provide the relevant code...I just thought I would explain from a higher level before doing so.  Any suggestions would be appreciated.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Strange error regarding buttons that are disabled
« Reply #1 on: August 04, 2013, 12:35:38 AM »
My guess is that your "is enabled" state gets overwritten shortly after you set it. You need to run your code after UIButton's OnEnable.

prinky

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Strange error regarding buttons that are disabled
« Reply #2 on: August 04, 2013, 12:52:52 AM »
My guess is that your "is enabled" state gets overwritten shortly after you set it. You need to run your code after UIButton's OnEnable.

You're referring to the isEnable = false code, right? I'm not sure which code would cause the button to enable itself. I should clarify that the gameobject with the UI Panel (A) is a parent to the buttons, and it gets disabled after the tween alpha operation is complete (since the tweener calls a function that does that).

Also the strange thing is that this problem only occurs when the panel is fading for the first time after the scene loads. The problem doesn't repeat itself.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Strange error regarding buttons that are disabled
« Reply #3 on: August 04, 2013, 07:03:49 AM »
I mean the general OnEnable call. All scripts get this call when they get enabled. UIButton does some things in its OnEnable which likely conflicts with you setting the button's isEnabled state before it.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Strange error regarding buttons that are disabled
« Reply #4 on: August 04, 2013, 10:00:45 AM »
Which version of Unity are you using, and which version of NGUI?

prinky

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Strange error regarding buttons that are disabled
« Reply #5 on: August 05, 2013, 11:30:42 PM »
Which version of Unity are you using, and which version of NGUI?

I am using Unity 4.1.4f1, and NGUI version 2.6.3.

I'm not sure how OnEnable would undo what I am doing, and if it is being called after the buttons are being disabled, but I can check.

prinky

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 13
    • View Profile
Re: Strange error regarding buttons that are disabled
« Reply #6 on: August 06, 2013, 12:59:38 AM »
I mean the general OnEnable call. All scripts get this call when they get enabled. UIButton does some things in its OnEnable which likely conflicts with you setting the button's isEnabled state before it.

I've placed breakpoints at the appropriate places (the setter and OnEnable), and nothing seems out of the ordinary there. In other words, those functions don't undo my code in any way.

It turns out the problem doesn't relate to panel A or panel B, but their parent (yes, the menu system is a bit complicated). Panel A and B are children of a "root menu" that can fade if the user wants to view the options or help screens. When the root menu fades in (shows up), there is a function (event receiver) that enables all the buttons of its children by default.  This global enabler function is only called once when the root menu is enabled, so it doesn't normally cause problems when moving between panels A and B. Note that if the root menu is enabled and panel B is hidden (which is the default), that panel won't work even though its buttons are enabled.

But if one were to click one buttons of panel A when that panel is fading out, that root's enabler is called somehow. A bit strange, but it happens. Edit: ah, figured out what's happening. The user can click a button from panel A before the root's tween function calls the enabler function (basically as the root menu is "fading in"). So would could happen is that panel A's buttons can be clicked by the user before that enabler function is called. As I said, they are disabled when they are clicked...but in this scenario the root's tween finishes and the enabler is called, which re-enables the buttons.
« Last Edit: August 06, 2013, 02:04:00 AM by prinky »