Author Topic: SetActive not re-enabling buttons correctly  (Read 16949 times)

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
SetActive not re-enabling buttons correctly
« on: January 29, 2013, 05:05:23 PM »
First time using NGUI even though I bought it a while ago.  I'm building a strategy game prototype and I want menus to appear as you click on certain units.  On those menus will be buttons that are contextual to the unit.  What's going on is that I can use SetActive to activate the SlicedSprites and Buttons once.  After they're SetActive(false) and then SetActive (true) again, the buttons never appear again (Can't see or interact with them).  Here's the kicker, if you actually CLICK on the buttons the first time you've selected a unit, click off the unit and click on it again, the buttons WILL render.  I'm completely baffled as to what's going on here.

Here's an example:  http://www.evil-lab.com/unity/StarshipCommand/StarshipCommand.html
  • Click on the Starbase (The spindle looking thing)
  • See the menu at the bottom of the screen
  • Click into empty space deselecting the unit
  • Click the Starbase again

The two sliced sprites making the menu frame show up but the buttons never appear again.  (As mentioned above, they WILL appear if you click on them the first time they show up.

Here's my set up:


The Starbase Build Anchor is the anchor, with the panel above it.  Below it are the 3 buttons and 2 sliced sprites.  I'm calling SetActive(true) on the Starbase Build Anchor when the unit is selected and SetActive(false) on it when it is deselected.  When I run in editor and click on the Starbase, the Hierarchy shows the buttons as active, they're just not there.  Am I doing some bad voodoo or am I missing something?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #1 on: January 29, 2013, 05:31:14 PM »
Are you sure they don't appear? Perhaps they appear but alpha is 0, making them invisible?

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #2 on: January 29, 2013, 06:56:03 PM »
Thanks for replying ArenMook.  I checked and the alpha isn't zero, here's 4 screenshots from the editor showing the inspector data:

As in the example above, the first time you click the Starbase:  http://www.evil-lab.com/unity/StarshipCommand/4_Before.png

After deselecting and selecting the Starbase:
UIButton:  http://www.evil-lab.com/unity/StarshipCommand/1_UIButton.png
Background:  http://www.evil-lab.com/unity/StarshipCommand/2_ButtonBackgroud.png
Label:  http://www.evil-lab.com/unity/StarshipCommand/3_Label.png

The alpha is 255.  Everything *looks* ok to me, but if I could catch what I'm missing, I'd be able to fix it  ;)

What other data would be useful to get to the bottom of this?

-Masterhyjinx

*edit*  Changed img tags to url because the images are big.
« Last Edit: January 29, 2013, 06:57:41 PM by masterhyjinx »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #3 on: January 29, 2013, 10:38:38 PM »
Try as I may, I just can't seem to reproduce this one. I start a new scene, create a button, disable it, hit play, enable it -- it works fine. Can you create a very simple repro case and send it to support at tasharen.com?

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #4 on: January 30, 2013, 01:28:33 AM »
I completely stripped this down with the same result.  Makes me feel a bit better after I stripped out all my stuff.  I'm not ruling out that it's my fault somehow but if there is a problem I created, I can't find it.  I sent you an e-mail with the details.  Thank You.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #5 on: January 30, 2013, 03:00:53 AM »
Could it just be hiding behind your background? Try giving it a z of -1.

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #6 on: January 30, 2013, 03:16:10 AM »
Could it just be hiding behind your background? Try giving it a z of -1.

I attempted to move the z -1 while running and before running.  Same issue persisted. 
I even re-imported NGUI to make sure I had the latest version.  I sent a very simple example illustrating the problem with my set up to support.

PapaWhiskey

  • Guest
Re: SetActive not re-enabling buttons correctly
« Reply #7 on: January 30, 2013, 12:42:52 PM »
could be a timing issue of when you "set active"...i have a similar bug (still), with labels, where depending on when the label is enabled, NGUI, while it says the label is active, if you look at the parenting panel, you can see says "zero draw calls"...check your parent panel...see if the panel is saying something like "two widgets, zero draw calls"...i've had to debug more urgent stuff so havent had time to dig into NGUI to see where the bug is, or why the bug manifests in NGUI with the way I'm using it...but maybe yours is similar...ymmv

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #8 on: January 30, 2013, 02:17:19 PM »
could be a timing issue of when you "set active"...i have a similar bug (still), with labels, where depending on when the label is enabled, NGUI, while it says the label is active, if you look at the parenting panel, you can see says "zero draw calls"...check your parent panel...see if the panel is saying something like "two widgets, zero draw calls"...i've had to debug more urgent stuff so havent had time to dig into NGUI to see where the bug is, or why the bug manifests in NGUI with the way I'm using it...but maybe yours is similar...ymmv

The first time everything activates properly I see 8 widgets, 1 draw call.  After deactivating and then activating again, I still see 8 widgets and 1 draw call.  I even attempted to enable/disable just a button.  2 widgets, 1 draw call.  After if fails to activate I still see 2 widgets, 1 draw call.  It's kind of a bummer because I was using NGUI to help me do the 1 Game a Month Jam (http://onegameamonth.com/) but this is holding me up.  I'm going to dive into NGUI's code tonight after work to see what I can find because theres still that oddity that if you mouse over the button the very first time it is active, the button can be activated and deactivated to my hearts content and it will work.

gekido

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 18
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #9 on: January 30, 2013, 02:39:33 PM »
are you using NGUITools.SetActive() or the native unity GameObject.SetActive() function?  I've found that the new unity SetActive function is sketchy - it's difficult to predict what will actually happen when you call it.  The fact that they removed the 'SetActiveRecursively' is extremely frustrating.

We've had a few situations where if a button was disabled by default, calling SetActive on the parent object won't actually enable the child objects etc.  Annoying to say the least.  In our case, we ended up cycling through the child objects and manually enabled them to work around this.

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #10 on: January 30, 2013, 04:46:28 PM »
are you using NGUITools.SetActive() or the native unity GameObject.SetActive() function?  I've found that the new unity SetActive function is sketchy - it's difficult to predict what will actually happen when you call it.  The fact that they removed the 'SetActiveRecursively' is extremely frustrating.

We've had a few situations where if a button was disabled by default, calling SetActive on the parent object won't actually enable the child objects etc.  Annoying to say the least.  In our case, we ended up cycling through the child objects and manually enabled them to work around this.

Yes I am using NGUITools.SetActive().

  1. public void OpenStarBaseMenu()
  2. {
  3.         NGUITools.SetActive(StarBaseGUI, true);
  4. }
  5.        
  6. public void CloseStarBaseMenu()
  7. {
  8.         NGUITools.SetActive(StarBaseGUI, false);
  9. }
  10.  

I would cycle through the child objects as well but when I attempted to use NGUITools.SetActive() on just ONE button it produces the same result.  The button is the same button that came out of the Widget tool aside from changing the colors and the text.  I feel at this point its something I'm doing because no one else seems to have this problem.  After I look into it tonight, I may just have to use a work around or revert back to Unity's built in system.  I just hated the idea of a work around involving moving the UIPanels around because it isn't WYSWIG easy anymore.

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #11 on: February 03, 2013, 05:47:33 AM »
Ok, I'm not sure how everyone around me can't seem to repro the problem but I found a work around that I don't quite understand yet.  I found that I can reproduce the issue super easily but creating a button and during run time, just checking and unchecking active check mark in the editor.  When I do this, the button disappears and when it is activated again, it doesn't actually render.

So I tracked down the issue to the UIButtonScale and UIButtonOffset scripts.  More specifically on the OnEnable() and OnDisable() handlers

  1. // UIButtonScale
  2.         void OnEnable ()
  3.         {
  4.                 if (mStarted && mHighlighted)
  5.                         OnHover(UICamera.IsHighlighted(gameObject));
  6.         }
  7.  
  8.         void OnDisable ()
  9.         {
  10.                 if (tweenTarget != null)
  11.                 {
  12.                         TweenScale tc = tweenTarget.GetComponent<TweenScale>();
  13.  
  14.                         if (tc != null)
  15.                         {
  16.                                 tc.scale = mScale;
  17.                                 tc.enabled = false;
  18.                         }
  19.                 }
  20.         }
  21.  
  22. // UIButtonOffset
  23.         void OnEnable ()
  24.         {
  25.                 if (mStarted && mHighlighted)
  26.                         OnHover(UICamera.IsHighlighted(gameObject));
  27.         }
  28.  
  29.         void OnDisable ()
  30.         {
  31.                 if (tweenTarget != null)
  32.                 {
  33.                         TweenPosition tc = tweenTarget.GetComponent<TweenPosition>();
  34.  
  35.                         if (tc != null)
  36.                         {
  37.                                 tc.position = mPos;
  38.                                 tc.enabled = false;
  39.                         }
  40.                 }
  41.         }
  42.  
  43.  

I noticed that the components set the scale to mPos which is (0,0,0), OnDisable() as well as setting the local position to (0,0,0) as well.  When the button is enabled again I went to look at the properties and there it was, the button's scale was 0.    If I restore it to 1 I can see the button again but it isn't in the correct spot.  (You can verify this by debugging and seeing that mPos is set to 0,0,0 as well as mScale)

As for a work around I found that if I remove the TweenScale script as well as the TweenPosition script from the button, you can disable and re-enable the button and it will appear correctly and continue to function correctly.  This seems to work fine because the components are added to the button as soon as they are needed.  I haven't tied the two together at the time that I write this but I may spend some time looking into it.

Also, if you look at one of the first screen shots I posted, you can see the scale being 0.  Are any of the devs around to verify the logic of how this should work?
http://www.evil-lab.com/unity/StarshipCommand/1_UIButton.png
« Last Edit: February 03, 2013, 06:00:23 AM by masterhyjinx »

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #12 on: February 03, 2013, 06:09:39 AM »
Alright, it looks like when I set a breakpoint in the Init() function of UIButtonScale and UIButtonOffset, the break never occurs.  Well it doesn't occur UNTIL you hover over a button, THEN and only then is Init() called.

  1. // UIButtonScale
  2.         void Init ()
  3.         {
  4.                 mInitDone = true;
  5.                 if (tweenTarget == null) tweenTarget = transform;
  6.                 mScale = tweenTarget.localScale;
  7.         }
  8.  
  9. // UIButtonOffset
  10.         void Init ()
  11.         {
  12.                 mInitDone = true;
  13.                 if (tweenTarget == null) tweenTarget = transform;
  14.                 mPos = tweenTarget.localPosition;
  15.         }
  16.  

So this is consistent with what I found so far.  IF you do not hover over the button and you disable it, the localPosition and localScale are NEVER recorded.  So when you enable the button, both are set to zero.  However, if you hover over the button, Init is called, the buttons localPosition and localScale are recorded and thus, not clobbered OnDisable().  I didn't write NGUI or so I can't say why it was architected this way.  So with just the knowledge I've gained thus far, the fix seems to be to put Init() in the Start() method for each of the two scripts.  I played around with it for a while after making this change and it seems to function properly.  I noticed that the UIButtonColor script does the same thing on Awake().

So my fix for now is:
  1. // UIButtonScale
  2.         void Start ()
  3.         {
  4.                 mStarted = true;
  5.                 Init();
  6.         }
  7.  
  8. // UIButtonOffset
  9.         void Start ()
  10.         {
  11.                 mStarted = true;
  12.                 Init();
  13.         }
  14.  

Author(s) of NGUI, what is your input on this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #13 on: February 03, 2013, 08:37:37 AM »
Why is local position and scale zero? Disabling it shouldn't set anything to zero. Zero scale is invalid, by the way -- causes division by zero errors.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #14 on: February 03, 2013, 08:45:24 AM »
My question aside though, the reason Init() was done like that was due to a bug in Unity that existed back in 3.5.0 days that has since been fixed. I will move all of it to Start() and see if it has any negative effects.