Is this by design that onValueChanged is raised even if the state of a toggle wasn’t changed (i.e. click again on a selected toggle in the group)?
It’s more like ‘undefined behaviour’ then a bug. We haven’t had an internal discussion about if it should or should not. If you have a strong feeling about this feel free to let us know the reasoning.
On way to work around this is to check if the value has changed. Based on some of the discussion I’ve seen there does seem to be a desire to have an event fire if a toggle is clicked without changing it.
ortin - have a look at the end of my thread, i have code example of how do i handle checkbox toggle event
Actually we did have internal discussion about this, though it’s a while ago. The events are fired regardless of whether the state is changed or not and this is by design. This allows controls build on top of ToggleGroup (for example a dropdown list) to detect a selection and close accordingly, even if the selection is the same as the existing one.
I got double calls using OnValueChanged and added an EventTrigger and used OnClick to get it to fire only once.
Can you file a bug report with an attached project that reproduces the issue (calling the event twice)? Thanks.
I also got double calls. Was this solved in some later version? Currently using version 4.6.0f1 ![]()
Edit: also “fixed” it with EventTrigger->PointerClick now, although not sure if this is intended behaviour…
I too find it strange that this triggers even if the value did not change. If that opens up possibilites then I don’t mind.
I would still suggest to rename this option so it does not confuse people. I don’t know if there is a function or an event with that name involved too, in which case it would be too late to still change it, but at least in the inspector it could be made a bit more clear and it should be explicitly mentioned in the docs. “On Value Changed” is very definite in it’s meaning and people tend to understand it exactly that way (only triggered IF the value actually changed).
Double calls: Are you perhaps using a toggle group with 2 toggles and a callback set up for both? If so then it’s no surprice that you get 2 calls. (This might probably not be it, but I mention it just in case).
This might also be a good moment to mention that I really love the new UI-System. I’m working with it now for a week and it’s been a bliss so far. Easy to use and it just works! → Very Unity ![]()
I’d agree with antx. If the behaviour of the functions being called when a toggle in the group is effectively reselected then the Event name needs to be changed as OnValueChanged would explicitly suggest that the methods should only be called if the value of the toggle changes, which in this case it does not.
Yesterday I told my wife that I changed my mind about ordering pizza. Secretly, my preference to order pizza still existed. Later when I asked why the pizza had not arrived yet, she was completely confused. It didn’t take much of an internal discussion to determine that I was failing to communicate clearly.
I came across this bug today. It’s called: "OnValueChanged " NOT “OnToggleClicked”. Either poor design or poor naming. Either the name should be changed to “OnToggleClicked” OR the behavior should change to be correct.
Using Unity 5.1.2. Am having two toggles in a toggle group say A and B. Now the problem is if i click on an active toggle say A(1), though A remains in toggled state and B OFF, the OnValueChanged(Boolean) function of A is fired. And if i click on B(0), B toggles on and OnValueChanged(Boolean) function of A is still fired. So it seems to be a mixture of OnValueChanged and OnClicked.
@Tim-C I think that is right.
love how this is still a thing.
still a thing ![]()
That’s one of reasons why Unity UI is a pain.
This. Why not have both? OnValueChanged means that something well, changed. It shouldn’t be called when double clicking a toggle.
I’m going to add to this even if it’s an old thread. My issue is that I am setting up my UI screen perhaps before it is enabled. So what’s happening is my initialization gets overwritten by the default state in the editor. I’m going to try to initialize in OnEnable to see if I can initialize the toggle without being overwritten.
EDIT: I said a bunch of stuff in edits that were mistaken. Let me warn you away from what I did. I wanted a toggle with the same size and format as one that was already part of a toggle group, so I copied it and pasted it where I wanted. However, the toggle is not a self contained object, and it remembers what toggle group it is in! So you copy it make sure to set this field to ‘None’ so it doesn’t just mess everything up.