Search Unity

Toggle onValueChanged called even if toggle state wasn't changed

Discussion in 'UGUI & TextMesh Pro' started by ortin, Aug 27, 2014.

  1. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    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)?
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,225
    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.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  4. Briksins

    Briksins

    Joined:
    Jul 15, 2012
    Posts:
    27
    ortin - have a look at the end of my thread, i have code example of how do i handle checkbox toggle event
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    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.
     
    Tim-C and ortin like this.
  6. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    I got double calls using OnValueChanged and added an EventTrigger and used OnClick to get it to fire only once.
     
    ShantiB95 and Ricks like this.
  7. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Can you file a bug report with an attached project that reproduces the issue (calling the event twice)? Thanks.
     
  8. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    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...
     
    Last edited: Dec 14, 2014
  9. antx

    antx

    Joined:
    Feb 16, 2012
    Posts:
    28
    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 ;-)
     
  10. Dover8

    Dover8

    Joined:
    Aug 20, 2010
    Posts:
    94
    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.
     
  11. UIGuy

    UIGuy

    Joined:
    Jan 22, 2015
    Posts:
    7
    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.
     
  12. DaveInNJ

    DaveInNJ

    Joined:
    Sep 22, 2015
    Posts:
    1
    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.
     
  13. Peace2all

    Peace2all

    Joined:
    Apr 4, 2015
    Posts:
    1
    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.
     
    Davon92 likes this.
  14. imDanOush

    imDanOush

    Joined:
    Oct 12, 2013
    Posts:
    368
    @Tim-C I think that is right.
     
    Last edited: Feb 19, 2018
    Davon92 likes this.
  15. Davon92

    Davon92

    Joined:
    Oct 24, 2014
    Posts:
    17
    love how this is still a thing.
     
    XazeRekt and ShantiB95 like this.
  16. ShantiB95

    ShantiB95

    Joined:
    Feb 8, 2017
    Posts:
    17
    still a thing :)
     
    XazeRekt likes this.
  17. XazeRekt

    XazeRekt

    Joined:
    Aug 21, 2016
    Posts:
    19
    That's one of reasons why Unity UI is a pain.
     
    ShantiB95 likes this.
  18. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    This. Why not have both? OnValueChanged means that something well, changed. It shouldn't be called when double clicking a toggle.
     
  19. cephalo2

    cephalo2

    Joined:
    Feb 25, 2016
    Posts:
    263
    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.
     
    Last edited: Oct 20, 2023