Author Topic: Problem with NGUITools.SetActive  (Read 5991 times)

Grhyll

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Problem with NGUITools.SetActive
« on: October 09, 2012, 10:05:23 AM »
Hello !

So until now I used NGUITools.SetActive without any problem, everything worked perfectly, but I just encountered a problem which I cannot figure out...
I have a menu Panel which I activate on some event, a scrollView in it, and in this scroll view, two game objects for two sections.
The second one contains a "title" with a UISlicedSprite and a UILabel, and when I try to desactivate this one, there is nothing to do, it just keeps being here. I guess I call some functions in the wrong order, but I cannot find a solution.

Here is the simplified script attached to the parent panel:

  1.         void OnEnable()
  2.         {
  3.                 NGUITools.SetActive(gameObject, true); //Had to do this because I need the children to be activated for the next functions
  4.                 RemoveAllGames();
  5.                 LoadGames();
  6.         }
  7.  
  8.         void LoadGames()
  9.         {
  10.                 //Here I insert a lot of objects in the first section of the scrollView
  11.                
  12.                 //And now I handle the second section:
  13.                 if(trueCondition)
  14.                 {
  15.                         NGUITools.SetActive(SecondSectionOfTheScrollView, false);
  16.                 }
  17.         }
  18.  
And after that the second section stays active, no matter what...

Maybe it is because I call it from a "OnEnable" function, but I can't find another solution (because I need this view to be reloaded each time the panel becomes active)... Any idea?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem with NGUITools.SetActive
« Reply #1 on: October 09, 2012, 05:29:35 PM »
The problem is in you calling it from OnEnable. Unity sends the enable event, you do your stuff, disable everything, then Unity continues calling OnEnable on all the children that you just disabled.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Problem with NGUITools.SetActive
« Reply #2 on: October 10, 2012, 02:53:32 AM »
A quick tip for getting the same effect that you have now, but working, is to set a bool in OnEnable and read it in Update and run the appropriate methods.

  1. bool turnedOn = false;
  2. void OnEnable()
  3. {
  4.   turnedOn = true;
  5. }
  6.  
  7. void Update()
  8. {
  9.   if (turnedOn)
  10.  {
  11.   turnedOn = false;
  12.   RemovedAllGames();
  13.   LoadGames();
  14.  }
  15. }
  16.  

Grhyll

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 32
    • View Profile
Re: Problem with NGUITools.SetActive
« Reply #3 on: October 10, 2012, 02:55:37 AM »
That's what I understood while going to bed (amazing how things are clearer when nearly asleep!)...

Thank you Nicki for the advice, I guess I'll have to do this way!