- Home /
How to control NGUI button by script
Many page said you just modify UIToggle.vale. It'll change all button status and call Callback function automaticly. It's work fine when My Ap start running and initial function set the UIToggle.vale to default value. But It make very strange when control by another Button's Script. All flag has be setting but CallBack and Active/Deactive Object not following! Any one knew loss something?
Thank you for your answer. Sorry, not really clear.
Seems like code:
//two groups button
// 1. ABC buttons
// 2. 123 buttons
public void ABCBtnValueChange(){ // attach on UIToggle of ABC button "On Value Change"
// when click button ABC It'll change button 123 status.( button 123 on)
if( UIToggle.current.value) {
Btn123UIToggle.value = true;
Btn123UIToggle.value = false;
}
}
Nothing change on Button 123! When I trace over "Btn123UIToggle.value = false" the UIToggle.value is not change to false!. nothing change in Button 123.( no Highligh, no calling "On Value chage" callback of Button 123
Yes! I have founded problem. We can't setting UIToggle value of Button-123 when button-ABC be press at same time(frame). Just move to next frame that button-ABC all event has been process. The code must be change to :
void Update()
{
if(button-123 light-on Require)
{
setting UIToggle value of Button-123 to "true".
Off button-123 light-on Require flag.
}
}
Answer by b1gry4n · Nov 04, 2014 at 05:11 AM
UIToggle.current.value = true;
UIToggle.current.value = false;
You can create a reference to the UIToggle and update that specific toggle script.
public UIToggle myToggle;
myToggle.current.value = true;
myToggle.current.value = false;
In the UIToggle script you need to link it to a function on value change. This is located under the "On value change", "Notify"
create a script containing:
public void ToggledValue(){ //name whatever you want
if(UIToggle.current.value){
//the value is true, do something
}else if (!UIToggle.current.value){
//the value is false, do something
}
}
Drag the script containing the "ToggledValue" function into the UIToggle component under notify and select the function "ToggledValue" so that when the UIToggle changes, it will call that function