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

Multiple Toggle Groups Not Working

Discussion in 'UGUI & TextMesh Pro' started by chuakc92, Dec 28, 2019.

  1. chuakc92

    chuakc92

    Joined:
    Nov 22, 2016
    Posts:
    20
    I'm trying to get something like this working:
    Code (CSharp):
    1. ---Some Category---
    2. [x] Some Category Option 1
    3. [ ] Some Category Option 2
    4.  
    5. ---Some Other Category---
    6. [ ] Some Other Category Option 1
    7. [x] Some Other Category Option 2
    Where the options look like buttons/selections and the selected ones stay "highlighted". I've tried using buttons, but there's no way of changing the state of the button to the highlighted state except referencing the button's image and changing it to the state's highlighted sprite. But this messes up the sprite swap after. I've tried using Toggles, but even using multiple Toggle Groups, you can only select one toggle at a time. Even the ones from other toggle groups get deselected. Is there really no way of doing this?

    Documentation says you can have more than one, but why is it not working then?
    Toggle Groups are useful anywhere the user must make a choice from a mutually exclusive set of options. Common examples include selecting player character types, speed settings (slow, medium, fast, etc), preset colors and days of the week. You can have more than one Toggle Group object in the scene
    at a time, so you can create several separate groups if necessary.



    panel
    - toggle group
    - toggle 1
    - toggle 2
    panel
    - toggle group
    - toggle 1
    - toggle 2
     
  2. miik2

    miik2

    Joined:
    Aug 23, 2018
    Posts:
    5
    Same problem here, do you find any solution?
     
  3. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    did you assign the correct toggle groups to the toggles?
     
  4. gavinlpicard

    gavinlpicard

    Joined:
    May 14, 2020
    Posts:
    1
    also having the same issue :/
     
  5. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    I didn't got the answer to my question before, so, maybe you can answer it for your case @gavinlpicard ...
     
  6. RosyMaple

    RosyMaple

    Joined:
    Dec 15, 2015
    Posts:
    3
    Also encountering this bug,
     
  7. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    okay, then please also post the unity version where the bug appears.
     
  8. nonabonasonadona

    nonabonasonadona

    Joined:
    Dec 8, 2014
    Posts:
    17
    I'am having this issue as well. I have two ToggleGroups. One toggle group is assigned to 2 toggles. And the other toggle group is assigned to 4 toggles.

    Clicking one toggle, un-toggles any of the other ones regardless of the groups assigned
     
    HuldaGnodima likes this.
  9. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    This is finally a description of the bug.
    I tried to reproduce it with Unity 2019.3.3f1 but it works as expected. Which Unity version are you using?
     
  10. nonabonasonadona

    nonabonasonadona

    Joined:
    Dec 8, 2014
    Posts:
    17
    I am using Unity 2019.3.14f1
     
  11. nonabonasonadona

    nonabonasonadona

    Joined:
    Dec 8, 2014
    Posts:
    17
    I just tested this issue in Unity 2019.4.1f1 and it seems to work there.

    Anyway to fix this issue in 3.14?
     
  12. mc184

    mc184

    Joined:
    Feb 16, 2016
    Posts:
    1
    Having this exact same problem here with version 2020.1.12. Has anyone encountered this in this version and is there a solution here?
     
    HuldaGnodima likes this.
  13. salmannabi

    salmannabi

    Joined:
    Oct 12, 2020
    Posts:
    4
    I am in the same boat and I have figured out a workaround to this problem, first allow me to explain what is happening

    Currently the way toggle groups work is you can have one active toggle at a time in any group, now what is happening in our situations is the sprites change, when we select a toggle the selected sprite of the button (or toggle) shows up but when we press anywhere else on the screen or another toggle from another group the selected toggle in the previous group changes its selection sprite to default state (the toggle's background sprite)

    In reality the toggle is still pressed its just that the sprite had to change because the user pressed somewhere else on the screen. You can check this by looking at the "isOn" state of the toggle while testing the toggle behaviour, the "isOn" state remains true (checked) for the selected toggle even if its sprite changes to default state after pressing somewhere else on the screen.
    This "isOn" is exactly what I used as a workaround.

    This is what I did

    Code (CSharp):
    1. public Toggle[] togglesArray = new Toggle[5]; // add all toggles to an array of the desired size
    // Now iterate over the "togglesArray" and change each toggle's sprite based on its "isOn" state

    Code (CSharp):
    1.  void UpdateToggleSprites()
    2.   {
    3.         int size = togglesArray.Length;
    4.         for (int i = 0; i < size; i++)
    5.         {
    6.             Toggle toggle = togglesArray[i];
    7.             if (toggle.isOn)
    8.                 toggle.image.sprite = toggle.spriteState.selectedSprite;
    9.             else
    10.                 toggle.image.sprite = toggle.spriteState.disabledSprite;
    11.         }
    12.     }

    // Make sure you add the default sprite to the disabled sprite state in the inspector for each toggle

    Now its upto you whether you want to call this method from void Update() or from void FixedUpdate(). I went for the later because it is run flat 50 frames per second instead of 60 or 120 frames based on device screen refresh rate

    Even still I forced the method to run only 10 frames per second within the FixedUpdate() so that the actual sprite swaps happen 10 times a frame instead of 50 times a frame, I couldn't see any flickering or delayed behaviour and it is less programatically intensive than 50 frames per second

    Here is the FixedUpdate() code
    // Declare "_lastSpriteUpdate" as a global float

    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         if (Time.time - _lastSpriteUpdate > 0.1)
    4.         {
    5.             _lastSpriteUpdate = Time.time;
    6.             UpdateToggleSprites();
    7.         }
    8.     }
     
    Last edited: Nov 28, 2020
  14. b_e_e_p2001

    b_e_e_p2001

    Joined:
    Jun 19, 2020
    Posts:
    2
    Unity 2019.4.15f1.
    Having this exact same problem . @@
     
  15. ged_games

    ged_games

    Joined:
    Aug 23, 2020
    Posts:
    5
    salmannabi likes this.
  16. salmannabi

    salmannabi

    Joined:
    Oct 12, 2020
    Posts:
    4
    Thanks "qoqgames", I have to say i did try the "Graphic" element in the toggle component but i was unable to assign an image sprite at the time, my mistake was I was assigning an image sprite directly but I guess you can only assign gameobjects with image components to this "Graphic" element and assign the required selectable sprite to this image object

    Doing this makes the toggles work as intended and no need to write additional code for it. Thank you!!!

    Edit: Found a much easier and better way, here it is

    When you create a toggle a "Background" object is created inside the "Toggle" object, inside this "Background" object is a "Checkmark" object. Both these Background and Checkmark objects have image components assigned to them. Just replace the checkmark image's "Source Image" to your desired selected state sprite and replace the background image's "Source Image" to the desired disabled state sprite, and thats all!!!

    Your Toggles will now work as intended, whether on their own or in a Toggle Group

    P.S. The checkmark object is already assigned to the Graphic element of the Toggle object so that's why it works without you needing to assign anything to the graphic element or creating additional image components

    Thank You! once again qoqgames
     
    Last edited: Dec 4, 2020
  17. Necrohunter

    Necrohunter

    Joined:
    May 18, 2017
    Posts:
    9
    hey there, so i had the same problem.
    i had an empty gameobject which had two childs and each childs had its own toggle group.
    if i press a toggle, the toggle from the other goup gets unchecked.

    so i found the problem:
    we all wanted to be smart and thought we dont need the checkmark :D

    so yeah, in my case i didnt need checkmarks, because i just wanted to color tint it.
    but it didnt work.
    so i used sprite swap.
    but it didnt work.
    so i put the checkmark back in and used spriteswap.
    in that case the child "background" is ur normal state of the toggle and the checkmark is the selected state.
    just change the checkmark with your image that represents the "checked" state of the button.
    thats it.
     
  18. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    This will not work properly. The transitions are only for selection states, not toggle states. when clicking a selectable (like a toggle). The selected state will remain as long as you don't click anywhere else.

    I created an Asset (Better UI - see signature) which allows you to assign individual transitions for toggle states. Without my asset you will have to listen to the
    onValueChanged 
    event and do the sprite swap in code.