Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Toggle - Changing isOn through script does not trigger onvaluechanged

Discussion in 'UGUI & TextMesh Pro' started by RakshithAnand, Sep 10, 2016.

  1. RakshithAnand

    RakshithAnand

    Joined:
    Jun 30, 2013
    Posts:
    56
    So when i try to change the toggle's is on through script.. on value changed is not triggered. Why?

    My toggles are part of a toggle group. Even Togglegroup.NotifyToggleOn does not do anything.
    Please need help.

    Thanks
     
    MagyarPeter likes this.
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Toggles do trigger OnValue Changed when you set their toggle direclty through script.

    You can test it by Creating a new Scene. Add a UI Button, UI Toggle and an Empty Game Object:
    Put this script on the Empty Game Object:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Text.RegularExpressions;
    4.  
    5. public class GenericTest : MonoBehaviour
    6. {
    7.    public Toggle myToggle;
    8.    public void ValueChanged()
    9.     {
    10.         Debug.Log("Value Changed");
    11.     }
    12.  
    13.     public void ChangeToggle()
    14.     {
    15.         myToggle.isOn = !myToggle.isOn;
    16.     }
    17.  
    18. }
    19.  
    Hook the Toggles On Value Changed to GameObject's Value Changed Method
    Hook the Buttons On Click to the GameObject's Change Toggle Method
    And make sure to drag the Toggle to the GameObject's Public myToggle in the editor.

    Then click the button you will see the Toggle turn on and off and you will get Debug Messages.

    What I expect is happening is whenever a Toggle is "On" and you set Toggle.isOn = true; this will NOT trigger an OnValue Changed.. because you didn't actually change the toggles value. (obviously the same thing occurs if its off and you toggle it off). If you code is depending on that OnValueChange to do certain things when a toggle is turned on, but its already turned on.. then thats where your error is.
     
    danosono and henryqng like this.
  3. RakshithAnand

    RakshithAnand

    Joined:
    Jun 30, 2013
    Posts:
    56
    Apparently it does not trigger in mine. I do change it similarly as you have done. But it never triggers on value changed.
    Weird! Using unity 5.3.6 p4

    Or maybe Im using dynamic toggle changed function suggested by Unity. Maybe there is an issue with that. Ill check with a normal function and get back
     
  4. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Also make sure your actaully changing the toggle. If its on and you set it to "on" it won't trigger an OnValueChanged.
     
  5. austinborden

    austinborden

    Joined:
    Aug 5, 2016
    Posts:
    24
    I thought I was having the same issue of OnValueChanged not getting called when updating isOn, but it was a Script Execution Order issue for me.
     
    ongtypc likes this.
  6. liaojiangzheng

    liaojiangzheng

    Joined:
    Dec 25, 2017
    Posts:
    2
    i think it is ugui not friendly.
    look below:
    public void NotifyToggleOn(Toggle toggle)
    {
    ValidateToggleIsInGroup(toggle);
    // disable all toggles in the group
    for (var i = 0; i < m_Toggles.Count; i++)
    {
    if (m_Toggles == toggle)
    continue;
    m_Toggles.isOn = false;
    }
    }
    so we should set targettoggle.isOn = true at first,
    then togglegroup.NotifyToggleOn(targettoggle)
     
    almarian98, a436t4ataf and Vukiz like this.
  7. Vukiz

    Vukiz

    Joined:
    Dec 11, 2014
    Posts:
    5
    Thanks, that helped!
     
  8. HyunMok_Moon

    HyunMok_Moon

    Joined:
    Oct 14, 2016
    Posts:
    24
    isOn set to true is not worked.
    I change state toggle button from script by this "Select" method.

    toggle.Select();
     
  9. yupeiliang

    yupeiliang

    Joined:
    Mar 28, 2018
    Posts:
    1
    On my test, I found the solution for this issue.
    Code (CSharp):
    1. Toggle_Currency.toggle.isOn = false;
    2. Toggle_Currency.toggle.isOn = true;
    ---------------------------
    In my situation, I want to set toggle.isOn = true to trigger onValueChange Method, but it doesn't work. So set toggle.isOn = false before it. This code can prepare a environment for toggle.isOn=true. In a word, If you want to trigger value changed, you must make the toggle change it's value.
     
    siddharth3322 likes this.
  10. unitylearn72

    unitylearn72

    Joined:
    Nov 22, 2019
    Posts:
    1
    Code (CSharp):
    1. private void changeToggleState(bool state) {
    2.             if (state) toggle.isOn = state;
    3.             else toggle.onValueChanged?.Invoke(state);
    4.         }
    You can use this method
     
    efge and Ievgen0101 like this.
  11. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    INCREDIBLY BIZARRE BUG IN 2020

    If you happen to have an inactive Toggle in the scene, everything goes wrong.
     
  12. thiagolrosa

    thiagolrosa

    Joined:
    Feb 22, 2017
    Posts:
    60
    @Fattie did you solve this issue?
     
  13. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    @pinkpointer unfortunately it's just a bad unity bug. you have to just manually turn them all on/off/etc when things change. there's no real solution as it's a bad unity bug :/
     
    supileem likes this.
  14. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    51
    Code (CSharp):
    1. toggle.isOn = true;
    absolutely triggers its OnValueChanged event for my project. It's quite frustrating. I'm not sure if it has something to do with the bug mentioned by @Fattie, but when my Server tells my clients to untoggle their toggles, the toggles toggle their toggle methods. :confused:
     
  15. Donutask

    Donutask

    Joined:
    Apr 24, 2019
    Posts:
    7
    Use
    Toggle.SetIsOnWithoutNotify()
     
    rbitard, efge and Kokowolo like this.
  16. mfatihbarut

    mfatihbarut

    Joined:
    Apr 11, 2018
    Posts:
    1,059
  17. Recluse

    Recluse

    Joined:
    May 16, 2010
    Posts:
    485
    This doesn't help my case - I got no problem with the user changing the toggle value. My issue is that I can't initialise the toggle value based on a variable. Nothing, including SetIsOnWithoutNotify() makes any difference.

    I want the toggle to be ticked or not, depending on whether a particular player setting is enabled. But setting the value in script does not alter the visual appearance of the toggle.

    Hmm, okay there seems to be some bug in Unity (2021.3.8f1). I have two toggles, identical setup - one of them is getting the changes from script, the other not responding. Both have the same enabled / disabled status when the script attempts to set them, both are set at the exact same time.
     
    Last edited: Sep 3, 2022
  18. ejoflo

    ejoflo

    Joined:
    Sep 15, 2021
    Posts:
    39
    Did you ever get this working? I'm having the exact same issue in 2021.3.10f. My toggles visibly turn off and on on all of my UI except one. No matter what I've tried, the toggle just does not allow for interaction. The value changes but the UI does not update whatsoever.
     
  19. sergeperepel

    sergeperepel

    Joined:
    May 23, 2019
    Posts:
    3
    I'm having the same problem in 2021.1.28f1. I have 24 toggles in my group and one of them is always working but the others not working if I that one is triggered.
     
  20. steigert

    steigert

    Joined:
    Jul 7, 2022
    Posts:
    1
    This is what worked for me:

    Code (CSharp):
    1. toggle.isOn = true;
    2. toggle.group.EnsureValidState();
     
  21. crossyfootball

    crossyfootball

    Joined:
    Sep 15, 2015
    Posts:
    3
    Did you ever get this working? I'm rocking in the same boat where one of my toggles simply refuses to be graphically updated via script. So annoying.
     
  22. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
    It's a 3+ yr old unity bug, so it's in the box where they will never bother fixing it
     
  23. patndave

    patndave

    Joined:
    Jan 16, 2016
    Posts:
    2
    This seems to be a good workaround (assuming you have the toggle in a group)
     
  24. JJunior

    JJunior

    Joined:
    May 22, 2019
    Posts:
    53
    Guys, I just realised the isOn does not trigger the onValueChanged if the Toggle is disabled. I had a similar issue where I was changing the isOn in the onEnable method and it was not working because it was not enabled (or was being enable). After I realised the the onValueChanged is only triggered if the Toggle is enabled (active) I was able to fix the issue I was having. I hope it helps ;)
     
    Edvard-D likes this.
  25. CSEliot

    CSEliot

    Joined:
    May 9, 2014
    Posts:
    33
    Hey! Wanted to contribute my input here as this thread comes up when searching "toggle ison not working".

    So I have data imported to the game at runtime. I only have ONE toggle per togglegroup then spawn more later via Instantiate dependent on the data downloaded.

    Instantiating a toggle assigned to a group does NOT register that toggle to the group. So after Instantiate i had to add

    "addedToggleComponent.group.RegisterToggle(addedToggleComponent)"

    EVEN THOUGH addedToggleComponent.group was not null, the togglegroup wasn't aware. For some reason a toggle with a non-null .group property wouldn't change state but also wouldn't throw an error.
     
  26. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,049
    This option worked for me :)
    Here is my code:
    Code (CSharp):
    1.  
    2. void OnEnable()
    3.     {
    4.         difficultyToggles[0].isOn = false;
    5.         difficultyToggles[1].isOn = false;
    6.  
    7.         if (PlayerPrefs.GetInt(GameModes.DIFFICULTY_MODE_STRING, GameModes.EASY_DIFFICULTY_MODE) == GameModes.EASY_DIFFICULTY_MODE)
    8.             difficultyToggles[0].isOn = true;
    9.         else if (PlayerPrefs.GetInt(GameModes.DIFFICULTY_MODE_STRING, GameModes.EASY_DIFFICULTY_MODE) == GameModes.STANDARD_DIFFICULTY_MODE)
    10.             difficultyToggles[1].isOn = true;
    11.  
    12.     }