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

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #15 on: February 04, 2013, 01:18:16 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.

Disabling it does set it to zero.  In UIButtonScale, when a button is disabled, the code sets tc.scale = mScale and mScale hasn't been initialized so it is simply (0,0,0).  Just set a breakpoint there and you'll see.  Init() was never called so mScale was never set to the tweenTarget.localScale.

The same goes for UIButtonOffset and mPos.  mPos is never set to the button's local position because Init() is never called.  The only time Init() is called is when you mouse over the button.  If you don't mouse over the button and disable the button, the bug manifests.

  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;   //<== This is (0,0,0) because Init() was never called
  17.                                 tc.enabled = false;
  18.                         }
  19.                 }
  20.         }
  21.  
  22.         void Init ()
  23.         {
  24.                 mInitDone = true;
  25.                 if (tweenTarget == null) tweenTarget = transform;
  26.                 mScale = tweenTarget.localScale;  //<== This will store the button's scale but ONLY after you hover over the button.
  27.         }
  28.  
  29.  
  30. // UIButtonOffset
  31.         void OnEnable ()
  32.         {
  33.                 if (mStarted && mHighlighted)
  34.                         OnHover(UICamera.IsHighlighted(gameObject));
  35.         }
  36.  
  37.         void OnDisable ()
  38.         {
  39.                 if (tweenTarget != null)
  40.                 {
  41.                         TweenPosition tc = tweenTarget.GetComponent<TweenPosition>();
  42.  
  43.                         if (tc != null)
  44.                         {
  45.                                 tc.position = mPos;   //<== mPos isn't initialized either until a hover over button...
  46.                                 tc.enabled = false;
  47.                         }
  48.                 }
  49.         }
  50.  
  51.         void Init ()
  52.         {
  53.                 mInitDone = true;
  54.                 if (tweenTarget == null) tweenTarget = transform;
  55.                 mPos = tweenTarget.localPosition;   //<== So until you do, localPosition will be (0,0,0)
  56.         }
  57.  

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 #16 on: February 04, 2013, 09:11:49 AM »
So in other words this problem can be avoided by having the prefab you are instantiating actually start as disabled to begin with rather than disable it right after instantiation.

masterhyjinx

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #17 on: February 04, 2013, 02:55:35 PM »
So in other words this problem can be avoided by having the prefab you are instantiating actually start as disabled to begin with rather than disable it right after instantiation.

Unfortunately this will not solve the problem because once the button is enabled and then disabled, OnDisable() will run setting the scale and local position to (0,0,0).  Starting Enabled or Disabled, Init() still hasn't run.  I haven't dove into NGUI to understand why Init() is only called when the button is hovered over but until the button's members are initialized properly, OnDisable() is going to set the localscale and position to 0.  As I said, my solution was to put Init() in the Start() so that when the button starts.

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 #18 on: February 04, 2013, 07:41:59 PM »
Ah yes, true.

Well in any case, I fixed this yesterday, so you will see it in the next update anyway.

coderkenny

  • Guest
Re: SetActive not re-enabling buttons correctly
« Reply #19 on: February 27, 2013, 11:11:53 AM »
First of all : I love NGUI, it's great, won't do any GUIs anymore in Unity without it  :)

but hit the same kind (?) of problem...

Actually Unity 4 disables the button (can see the checkbox unchecked when "debugged"). I tried to disable a button in Start() method attached to it and tried to enable it through sendMessage VIA a tag to the void restoreMe(); in the same script.
I also messed things more with timed coroutines maybe I tried to use messaging improperly. Or there is something I don't quite follow in Unity itself.

Quick-and-dirty :
// NGUITools.SetActive(GameObject.FindGameObjectWithTag("GameOver"),false);
GameObject.FindGameObjectWithTag("GameOver").transform.Translate(200,0,0); // Throw it out-of-sight

&

// NGUITools.SetActive(GameObject.FindGameObjectWithTag("GameOver"),true);
GameObject.FindGameObjectWithTag("GameOver").transform.Translate(-200,0,0); // Throw it back

Not very proud about this but was too tired to fight  :-\ don't remember what other I stunts I tried but there is no over-head issue in my case.
Been coding over 10 hours now so I won't start to a topic about the distorted fonts that haunts me. I'll Google that, I think my imported TGA's are misaligned with wanted sizes  :-X

Cheers, keep up the good work !

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #20 on: March 02, 2013, 01:04:06 PM »
You can't SendMessage to a disabled gameobject - it doesn't receive it.

Make a new post with your problems, so it's easier to tell them apart. :)

And get some rest.

coderkenny

  • Guest
Re: SetActive not re-enabling buttons correctly
« Reply #21 on: March 04, 2013, 02:04:52 PM »
Thanks a lot for this clarificatition !!

I didn't know this "limitatition". So it was kinda hard to isolate the problem at hand :)

I noticed the null reference errors but was unable to tell why the message was not received though it was sent.


Again learned from my mistake. I try to avoid starting new, unnecessery topics because I will most likely receive negative feedback if there a thread already exists. Been there :D

fwalker

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
Re: SetActive not re-enabling buttons correctly
« Reply #22 on: March 14, 2013, 09:07:30 PM »
I am seing the exact same problem as "masterhyjinx".  But it's related to the TweenScale Script rather that UIButtonScale.
The behaviour is the same. Once the tweenScale finishes I SetActive to false. Then next time the component is Activated all the UI conponents such as buttons (but sprites as well) are not drawn unless you hover with the mouse over them.

In which update can we expect a fix for this problem? Can you verify if TweenScale has also this problem?

masterhyjinx, have you been able to verify the fix?

Thanks
« Last Edit: March 14, 2013, 09:22:11 PM by fwalker »

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 #23 on: March 16, 2013, 11:15:44 AM »
This is an old topic and the issue has since been resolved. However if you tween something to 0, you will still run into this problem because scale of 0 is invalid.