0

I have two UIToggle objects, so when I click on, for example, toggle 1 it enables itself and toggle 2 becomes disabled. But I am trying to toggle them in the script.

I get the UIToggle 1 like this:

 UIToggle toggleOn = ToogleParen.transform.FindChild("ButtonOn").GetComponent<UIToggle>();

And activate it doing this:

toggleOn.value = true;

The problem becomes when I try to deactivate it:

toggleOn.value = false; <--- does nothing, the toggle button does not deactivate!!

Does anyone know how to toggle two UIToggle objects programmatically?

Thank you!!

Rune Vejen Petersen
  • 3,201
  • 2
  • 30
  • 46
Kane
  • 375
  • 1
  • 6
  • 18
  • Are you using this as a radio button? If so, NGUI tries to make sure that exactly one option in the group is enabled (in doing so, it might ignore an attempt to set `value` to false). You can bypass this by setting the group to zero, or setting `optionCanBeNone`. – rutter Oct 10 '14 at 18:53
  • Thanks for the answer rutter! optionCanBeNone does not allow false values, so the only way is to put the group to zero an eliminate the radio button behavior. Thanks a lot! : ) – Kane Oct 14 '14 at 01:21
  • If you do want them to behave as radio buttons however, then you just need to set which ever toggle is meant to be on to true, and the rest will update. – Dover8 Feb 16 '15 at 10:23

1 Answers1

0

You will have to refresh the control so that NGUI redraws it on the screen. Just like you have to do incase of a grid after adding certain items.

BONUS: By what you have described I think you want to achieve something like radio buttons (only one selection is possible at a time). If yes, then you should use UICheckbox as NGUI provides features to use checkbox as a radio button and i this case you won't have to uncheck other checkboxes in the code.

Anas iqbal
  • 1,036
  • 8
  • 23