Author Topic: NGUI.TOOls set active not working as intended ?????  (Read 15487 times)

sk1989

  • Guest
NGUI.TOOls set active not working as intended ?????
« on: July 24, 2012, 12:08:30 PM »
Below is the snippet of my code. I want to set "buyButton" to active when "button" is selected and set the "button" to false at the same time . Do this vice versa. But what i get is either both the buttons disappear off the screen or both the buttons showing on top of each other. Can you help me with this and how this can be achieved in NGUI.

If not, is there a possibility where I can change the depth value of the buttons so that I can change which button is on top of the other button.




if(IsSelected)
      {
          NGUITools.SetActive(buyButton.gameObject,true);
          Position = buyButton.transform.position;
          buyButton.transform.position = Position;
       
          NGUITools.SetActive(button.gameObject,false);
         
      }
      else if(IsSelected == false)
      {
         NGUITools.SetActive(button.gameObject,true);//this part does not work, it does not go back to true.why?
         NGUITools.SetActive(buyButton.gameObject,false);
         
      }


thanks in advance for anyone with the help

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #1 on: July 24, 2012, 01:32:03 PM »
I am attaching a pic for better understandment

PhilipC

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #2 on: July 24, 2012, 01:38:35 PM »
NGUITools.SetActive is acctually recursive so any children will also get disabled (Unity's setactiverecursivly doesnt enable and disable in the correct order). My guess is that your buy button is a child of the object you are trying to disable.

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: NGUI.TOOls set active not working as intended ?????
« Reply #3 on: July 24, 2012, 01:41:32 PM »
Either that or he's disabling the child that has the button image and the collider, but not the other pieces of the button.

sk1989:  give us a screencap of these buttons in your heierarchy view, with all the children expanded.

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #4 on: July 24, 2012, 01:49:57 PM »
here is the screen shot.
That's what i thought too, so i put those buttons in an empty game object.


JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: NGUI.TOOls set active not working as intended ?????
« Reply #5 on: July 24, 2012, 02:46:43 PM »
Are all of those "Child (clone)" items buttons?  If so, why aren't they parented to some NGUI panel that's parented to an anchor that's parented to a UICamera?  (Meaning, they certainly aren't going to behave right in your UI if some of your items are in NGUI's UICamera 2D space, and some are in your world main camera space.)
« Last Edit: July 24, 2012, 02:49:18 PM by JRoch »

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #6 on: July 24, 2012, 02:48:45 PM »
i don't know why they appear that way because, the way i have it setup is, im instantiate each item from a prefab with another script that is attached to the grid.

JRoch

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 140
    • View Profile
Re: NGUI.TOOls set active not working as intended ?????
« Reply #7 on: July 24, 2012, 02:51:14 PM »
You have to grab the top-most gameobject of your prefab and then set its transform.parent to your grid's gameobject.transform.  If the script is attached to the grid it will be something like:

  1. Transform newButtonItem = ((GameObject)Instantiate((GameObject)Resources.Load("Prefabs/Whatever/MyPrefab"))).transform;
  2. newButtonItem.parent = this.transform;
  3. newButtonItem.name = "InventoryButton_" + newButtonItem.GetInstanceID.ToString(); // make the name unique, just for laughs
  4.  
  5. // now call your code that re-calcs the grid layout...
  6.  
  7.  
« Last Edit: July 24, 2012, 02:58:01 PM by JRoch »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI.TOOls set active not working as intended ?????
« Reply #8 on: July 24, 2012, 05:16:33 PM »
Don't use Instantiate. Use NGUITools.AddChild.

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #9 on: July 24, 2012, 05:32:32 PM »
I have the instantiate stuff on a different script attached to the grid, where it instantiates the buttons.

the prefab that I have for to instantiate is basically has both the button and buyButton in it as child. Also i added another script to each buttons to detect
the onSelect() and broadcast the message to the script that is on the parent of these buttons . But now if i select any of the buttons in the list, they all get selected.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI.TOOls set active not working as intended ?????
« Reply #10 on: July 24, 2012, 05:37:43 PM »
Component.Broadcast sends the message recursively, including all child objects -- just like NGUITools.SetActive. You should use SendMessage if you want it to go only to the one object you're targeting. Consult Unity's documentation for more information.

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #11 on: July 24, 2012, 06:02:11 PM »
I did the send message instead of broadcast message. so now it does select the specific button. but now it does not set the button back to active . That works perfectly fine with the BuyButton. Here is the snippet of my code.

private void Display(bool isSelected)
   {
      _isSelected=isSelected;
      if(IsSelected)
      {   
         Debug.Log("here");
         Vector3 newpos = _panel.transform.worldToLocalMatrix.MultiplyPoint3x4(button.transform.position);
          SpringPanel.Begin(_panel.gameObject,-newpos,50.0f);
         
         NGUITools.SetActive(buyButton.gameObject,true);
         buyButton.transform.position = Position;
         NGUITools.SetActive(button.gameObject,false);
      
         _lbl.color = Color.red;

         
      }      
      else if (IsSelected == false)
      {   
         NGUITools.SetActive(buyButton.gameObject,false);
         NGUITools.SetActive(button.gameObject,true);// this part does not work for some reason.
         _lbl.color = Color.yellow;

         
            
      }
      
   }

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #12 on: July 24, 2012, 07:17:58 PM »
I know what the problem is, once set the button to false(not active) then after that it does not detect OnSelect function anymore. so how do I go around this.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI.TOOls set active not working as intended ?????
« Reply #13 on: July 24, 2012, 07:20:27 PM »
You can't. Disabled game objects can't receive events.

sk1989

  • Guest
Re: NGUI.TOOls set active not working as intended ?????
« Reply #14 on: July 24, 2012, 09:38:14 PM »
Sorry but, How else can I do this behavior for my situation. Does NGUI have anything that fix my problem? I was thinking of moving the button off screen when its selected instead of deactivating it. But it wont let me do that either because the position is being override by the grid position from the script that is attached to the grid.

IF anyone have a solution for this problem, thanks in advance.