Is there some way to avoid OnValueChanged firing when the toggle gets de-selected in ToggleGroup (when other toggle gets clicked)?
Whats the problem:
I have 3 toggle buttons, in same toggle group
When Toggle2 gets clicked, panel opens (good)
When Toggle3 gets clicked, then Toggle2 OnValueChanged gets fired and that toggle2 panel opens…(not good)
I’d like to use the Toggle component event list, instead of creating any scripts for handling these simple 3 toggle buttons.
(And i’m using toggle instead of normal button, so that the user can see which mode is selected from the 3 toggles…)
I am having the same issue. When I use the default OnValueChanged function, the code for one toggle gets called twice, once when it is selected and once when it is deselected (that is, when another toggle in the group is pressed). Does anyone know whether there is an easy way to make it so the code only runs when the toggle is pressed? Thanks!
Why not just check the toggle.isOn parameter to figure out if this one just turned on or off? There’s also a boolean variable received by the onValueChange handler, which tells it whether it was switched on or off…
Note it’s called “onValueChange” and not “onToggleOn”, so it make sense it will be called both when turning on and when turning off.
toggle.onValueChanged.AddListener((bool on) => {
if (on) {
// Open the panel
}
});
Thanks for the reply! Thus far I’ve been using the ‘On Value Changed’ field in the inspector to add listeners to my toggles, but in this case it’s not giving me as much control as I need. It sounds like the best thing to do would be to add listeners via script as you suggested so I can check if they are on. Thanks for the help!
You could still use the On Value Change from the inspector, just make sure your method handler receives a boolean param. (or if it can’t for some weird reason, just check the isOn parameter of the toggle…)
instead using OnValueChanged why not use EventTrigger - Pointer Click so it will only be called Once.
same as adding Listener via script but its more quick and handy.
This solved it for me too. Thanks @vfxjex for the help.
I tried using the OnValueChanged without success and it appears that “isOn” returns true when it is being switched off although I didn’t test this extensively.
As you can see, as @vfxjex suggested, I use an event trigger pointer click, so this will fire only once. This will call a particular function which handles the toggle. However, you may have to check if that was the last active toggle, which prevents for your function called on that toggle again.
private int activeToggleID;
public void Transition1(Toggle toggle)
{
if (toggle.GetInstanceID() == activeToggleID) return;
...
activeToggleID = toggle.GetInstanceID();
}
I think I came up with simple and neat solution. We can inherit from Toggle class and override It’s behaviour.
public class SilentToggle : Toggle
{
public override void OnPointerClick(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left)
return;
if (!IsActive() || !IsInteractable())
return;
// SetIsOnWithoutNotify updates assigned toggle group without calling OnValueChanged on toggles.
SetIsOnWithoutNotify(!isOn);
//We then call onValueChanged manually, so It's called only for this toggle
onValueChanged.Invoke(isOn);
}
}
That way we get a toggle which works like normal toggle, but doesn’t call OnValueChanged on other toggles.