Author Topic: UIToggle Not Changing value  (Read 5760 times)

ababilinski

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
UIToggle Not Changing value
« on: June 09, 2014, 11:12:17 PM »
  1. public void RestState(){
  2.                 if(CurButton != null){
  3.                  UIToggle _UI = CurButton.GetComponent<UIToggle>();
  4.                 if( _UI != null){
  5.                        
  6.                         CurButton.GetComponent<UIToggle>().value = false;
  7.                         Debug.Log(CurButton.GetComponent<UIToggle>().value);
  8.  
  9.                 }
  10.         }
  11. }
I am using this code above and I don't know what I am doing wrong. I get the code and a bool is supposed to set the value of the check-box back to false. However, It is not working.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIToggle Not Changing value
« Reply #1 on: June 10, 2014, 02:44:12 AM »
Where is this code getting called from? Also define "not working"? Do you even see the Debug.Log?

ababilinski

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UIToggle Not Changing value
« Reply #2 on: June 10, 2014, 07:36:08 AM »
Quote
Where is this code getting called from? Also define "not working"? Do you even see the Debug.Log?

the function is being called in another codes update function. Sorry for my vagueness.

The problem that is happening is that the value is not changing. I added a debug to see the current value and it is set to true. no mater what I do.

ababilinski

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: UIToggle Not Changing value
« Reply #3 on: June 10, 2014, 07:41:05 AM »
Also if I use a code like this

  1.         public bool turnOn;
  2.  
  3.         // Update is called once per frame
  4.         void Update () {
  5.                 if(turnOn == false){
  6.                 gameObject.GetComponent<UIToggle>().value = false;
  7.                 }
  8.  
  9.                 if(turnOn == true){
  10.                         gameObject.GetComponent<UIToggle>().value = true;
  11.                 }
  12.         }

then it will turn on using
  1. gameObject.GetComponent<UIToggle>().value = true;

but it will not turn off even when turnOn = false.

This also only happens when there is a group assigned.
« Last Edit: June 10, 2014, 07:47:26 AM by ababilinski »

Fireball14

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 1
  • Posts: 23
    • View Profile
Re: UIToggle Not Changing value
« Reply #4 on: June 10, 2014, 09:52:51 AM »
Remember if you want some option in toogle groupe to be always active, then specify what toggle shoud be active if all of them go off.
  1. public bool turnOn;
  2.    
  3.     void OnEnable()
  4.     {
  5.         GetComponent<UIToggle>().optionCanBeNone = true;  // <- This is your problem!
  6.         turnOn = GetComponent<UIToggle>().startsActive;
  7.     }
  8.     void Update()
  9.     {
  10.         GetComponent<UIToggle>().value = turnOn;
  11.     }
  12.  

Also remember to sendout status change on UIToggle value change event.
But as i see it, its a lot more easy to use toggle with out any other scripts and sendout   c# event on value changed or use NGUI EventDelegate system.
« Last Edit: June 10, 2014, 10:39:33 AM by Fireball14 »