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

Toggle OnValueChanged Parameter Doesn't Change

Discussion in 'UGUI & TextMesh Pro' started by kromenak, Sep 12, 2014.

  1. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    I'm trying to create a simple option toggle with the Toggle component. When I hook up a callback for OnValueChanged and run the game, I noticed that the callback always receives whatever value I set in the Toggle window in the inspector - either true or false?

    public void OnToggle(bool selected)
    {
    Debug.Log(selected);
    }

    Isn't the callback supposed to pass the current value of the toggle itself, not a constant boolean value?
     
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Make sure you select the method located under the "Dynamic bool" header.
     
  3. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    Ah, that'll do it! Don't know how I managed to miss that. Thanks!
     
    josetteseitz and jhhigginscgc like this.
  4. mix773

    mix773

    Joined:
    Oct 7, 2014
    Posts:
    3
    Got the same unobvious problem, thanks for time saved!
     
  5. tolosaoldfan

    tolosaoldfan

    Joined:
    Sep 14, 2012
    Posts:
    92
    Thanks for the solution, I just discovered the "dynamic" option that displays the function twice. You saved my day
     
    Lohoris2 likes this.
  6. r2digi

    r2digi

    Joined:
    Sep 26, 2013
    Posts:
    24
    OMG i was trying to figure this out for an hour and never noticed there was two sections LOL. thanks so much!
     
    Lohoris2 likes this.
  7. ownself

    ownself

    Joined:
    Mar 18, 2013
    Posts:
    16
    Thanks man
     
  8. Aseemy

    Aseemy

    Joined:
    Aug 22, 2015
    Posts:
    207
    3 years later and your post is still helping noobs like me. Thanks.
     
    J_Kost and User340 like this.
  9. Gotama

    Gotama

    Joined:
    Feb 16, 2014
    Posts:
    4
    Wow, I needed some minutes to figure out "where and what" dynamic bool header is.
    Hope this screen can save some minutes to somebody dynamicToggle.png .
     
  10. pev285

    pev285

    Joined:
    Dec 13, 2017
    Posts:
    1
    You saved time for me!
     
    lucbloom likes this.
  11. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Still going strong. This thread just rescued me, too.
     
  12. ZealousAppex

    ZealousAppex

    Joined:
    May 3, 2015
    Posts:
    17
    Thank you save me time!
     
  13. rubenpvargas

    rubenpvargas

    Joined:
    Jul 18, 2012
    Posts:
    34
    Thanks a lot!
     
  14. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    If only I got a dollar for every like.
     
  15. Jantmi

    Jantmi

    Joined:
    Mar 13, 2015
    Posts:
    3
    THANK YOU! STILL GOING STRONG!
     
  16. RobertoBubaloZutec

    RobertoBubaloZutec

    Joined:
    Aug 20, 2018
    Posts:
    3
    Woah, can not believe I have missed it as well.
    Many thanks for the help!
     
  17. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    140
    Old thread but still saved me, who worked with Unity for 5 years but didn't know there's a dynamic functions can be used in Toggle....
     
  18. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    676
    Hey, got anything for sale on the Asset Store?
     
  19. T4NK32

    T4NK32

    Joined:
    Aug 7, 2019
    Posts:
    16
    Me too - FIVE years later.
     
  20. AlejUb

    AlejUb

    Joined:
    Mar 11, 2019
    Posts:
    25
    Can someone confirm that it actually works?
    I have a simple toggle button that calls on the event "OnValueChanged" a call a GameObject.SetActive in dynamic mode and it doesn't work. If I use the static mode it does work.
    Unity 2019.3.0f6

    Edit: It won't work if fast play mode is enabled. It has to go through the normal full Domain/Scene etc reload when hitting play.
     
    Last edited: Jan 31, 2020
    marius_87 and Nothke like this.
  21. ktest112233

    ktest112233

    Joined:
    Jan 7, 2019
    Posts:
    37
    thanks buddy, fixed it for me
     
  22. Nothke

    Nothke

    Joined:
    Dec 2, 2012
    Posts:
    112
    Just had the same issue. Thanks for figuring out what causes the problem. Still not fixed in latest 2019.3.

    This is the workaround I made:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ToggleSetEvent : MonoBehaviour
    5. {
    6.     public Toggle.ToggleEvent onValueChangedOverride;
    7.  
    8.     void Start()
    9.     {
    10.         Toggle toggle = GetComponent<Toggle>();
    11.         toggle.onValueChanged = onValueChangedOverride;
    12.     }
    13. }
    14.  
    Just attach this to your Toggle and set the event in it, now it will register even without domain reload
     
    DK_A5B, marius_87 and AlejUb like this.
  23. florinel2102

    florinel2102

    Joined:
    May 21, 2019
    Posts:
    76
    For me worked
    Code (CSharp):
    1. toogle = GetComponent<Toogle>();
    2.  
    3. toogle.SetIsOnWithoutNotify(false);
    4.  
    5. toogle.onValueChanged.AddListener(value => // do code) ;

    I think default is without notify .
     
  24. weareroyale

    weareroyale

    Joined:
    Oct 23, 2014
    Posts:
    4
    Still useful in November 2020.
     
  25. fabiorondina82

    fabiorondina82

    Joined:
    May 20, 2020
    Posts:
    3
    Hi everyone, I am using it to change the value of a boolean on my script.

    I hooked it dynamically to my method but then when I try to use it as condition the value does not change dinamically.

    public void SetBool(bool isMarked)
    {
    isStepDone = isMarked;
    Debug.Log(isStepDone);
    }

    this is the other method that should use the bool as flag
    public void DisplayNextSentence()
    {
    Debug.Log(isStepDone);
    // check if there are more sentences on the Queue
    if (senteces.Count == 0 && isStepDone)//
    {
    // reached the end of the Queue
    EndDialogue();
    return;
    }

    each time the Debub.log tell me that is false
     
  26. Virtua-Sinner

    Virtua-Sinner

    Joined:
    Mar 4, 2021
    Posts:
    2
    Hasn't helped me.

    public void voBriefingToggle(bool VOBriefings)
    {
    voiceBriefings = VOBriefings;
    print(VOBriefings);
    AudioSource.PlayClipAtPoint(changePage, new Vector3(0, 0, 0));
    }

    VOBriefings toggles properly to false if I turn it off. It toggles properly to true if I turn it back on. But once I've done that? It can never be false again regardless of whether the toggle is checked or not.
     
  27. erikobob

    erikobob

    Joined:
    Jul 1, 2017
    Posts:
    3
    helped me
     
    Bananatoast likes this.
  28. Dave-Me

    Dave-Me

    Joined:
    Sep 3, 2013
    Posts:
    4
    Thanks! Dynamic bool for the win!
     
  29. axelben

    axelben

    Joined:
    Sep 27, 2019
    Posts:
    1
    You did thanks ! :)