Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Toggle, OnValueChanged gets called in togglegroup

Discussion in 'UGUI & TextMesh Pro' started by mgear, Jan 17, 2015.

  1. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,022
    hi,

    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..)
     
  2. achand004

    achand004

    Joined:
    Mar 17, 2013
    Posts:
    4
    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!
     
  3. Tomer-Barkan

    Tomer-Barkan

    Joined:
    Jul 31, 2012
    Posts:
    150
    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.

    Code (CSharp):
    1.             toggle.onValueChanged.AddListener((bool on) => {
    2.                 if (on) {
    3.                     // Open the panel
    4.                 }
    5.             });
    6.  
     
  4. achand004

    achand004

    Joined:
    Mar 17, 2013
    Posts:
    4
    Hey Tomer,

    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!
     
  5. Tomer-Barkan

    Tomer-Barkan

    Joined:
    Jul 31, 2012
    Posts:
    150
    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...)
     
  6. vfxjex

    vfxjex

    Joined:
    Jun 3, 2013
    Posts:
    93
    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.
     
  7. gegagome

    gegagome

    Joined:
    Oct 11, 2012
    Posts:
    389
    It's been a while but I was having the same issue and @vfxjex reply worked better than onValueChanged
     
    carsonherrick likes this.
  8. carsonherrick

    carsonherrick

    Joined:
    Dec 19, 2016
    Posts:
    6
    Agreed.
     
  9. Furious-Witch

    Furious-Witch

    Joined:
    Mar 1, 2015
    Posts:
    11
    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.
     
  10. Pimpace

    Pimpace

    Joined:
    Sep 21, 2017
    Posts:
    41
    This is nuts! :D

    my solution is:

    upload_2020-8-27_16-10-26.png

    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.

    Code (CSharp):
    1.  
    2. private int activeToggleID;
    3.  
    4. public void Transition1(Toggle toggle)
    5.     {
    6.         if (toggle.GetInstanceID() == activeToggleID) return;
    7.  
    8.         ...
    9.  
    10.         activeToggleID = toggle.GetInstanceID();
    11.     }
    12.  
    Hope this helps!
     
    mgear, christopher_hof and Extiward like this.
  11. Kondziu2504

    Kondziu2504

    Joined:
    May 28, 2018
    Posts:
    1
    I think I came up with simple and neat solution. We can inherit from Toggle class and override It's behaviour.

    Code (CSharp):
    1. public class SilentToggle : Toggle
    2. {
    3.     public override void OnPointerClick(PointerEventData eventData)
    4.     {
    5.         if (eventData.button != PointerEventData.InputButton.Left)
    6.             return;
    7.  
    8.         if (!IsActive() || !IsInteractable())
    9.             return;
    10.  
    11.         // SetIsOnWithoutNotify updates assigned toggle group without calling OnValueChanged on toggles.
    12.         SetIsOnWithoutNotify(!isOn);
    13.  
    14.         //We then call onValueChanged manually, so It's called only for this toggle
    15.         onValueChanged.Invoke(isOn);
    16.     }
    17. }
    18.  
    That way we get a toggle which works like normal toggle, but doesn't call OnValueChanged on other toggles.
     
    Monkis, shaico, Ony and 1 other person like this.