Get the active Toggle from a ToggleGroup

How do you guys get the active Toggle from a ToggleGroup?

I have a group with 6 different toggles for setting the quality level of my game (Fastest, Fast, Simple etc. etc.). I want to call a ApplyQuality() function when the player presses a button and set QualityLevel to whatever the player chose in the ToggleGroup but I can’t figure out how…

This from the Unity Docs might be what you’re looking for?

That doesn’t say much, really… no examples on how to use it or anything.

var toggle = qualityGroup.ActiveToggles().First();
        if (toggle == ???)
        {

        }

^ That’s a solution I’m looking for, but I don’t know what to put instead of the “???”…

Well, I’d guess the main purpose of the ToggleGroup is to have a built-in manager in order to provide radio-button funcionality. Your best bet is probably to track the toggles themselves and query their state with the isOn property or add an event listener that sets a state object, e.g. a string or an integer that indicates which settign has been chosen.

Other than that, why don’t you use a Dropdown? It saves some space and already provides the functionality to retreive the currently selected value as this is managed internally.

The only situation in which I’d make a use of radio-button functionality using a ToggleGroup would be if I were about to bundle toggles in a group which influence greater parts of the UI. And even then it would only function as a quick solution for “allow only one active toggle”-effect.

Yeah, that will probably be the right approach I guess. I was hoping there was something far more simplier :slight_smile:
A dropdown would do fine, but I have a lot of space to fill so radio-buttons will work great :slight_smile:

It’s actually simple :slight_smile:

I’ve written this quick example, which does several things:

  • provides an array to easily drop any toggle into it that should belong to a group
  • enforces to always add a toggle group (so that the radiobutton-functionality doesn’t need to be implemented)
  • automatically adds toggle to the group (could also be done in Awake at runtime - your choice)
  • subscriber method for the toggles ‘OnValueChanged’ event that takes an integer (representing the quality level)

Just drag&drop the toggles into the array, hook up the subscriber (SetQualityLevel) and pass in the corresponding integer that represents the QualityLevel.

I did not add many comments in the code for the sake of brevity.

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(ToggleGroup))]
public class RadioButtonSample : MonoBehaviour
{
    [SerializeField]
    private Toggle[] _toggles;
  
    public void SetQualityLevel(int qualityLevel)
    {
        // I'm just applying the settings here
        // you could also cache the value and apply it when
        // an "Apply Settings" button is pressed
        QualitySettings.SetQualityLevel(qualityLevel, false);
    }

    #region EDITOR ONLY
    private void OnValidate()
    {
        var toggleGroup = GetComponent<ToggleGroup>();
        for (var i = 0; i < _toggles.Length; ++i)
        {
            if (_toggles[i] == null)
            {
                var messageFormat = "Toggle #{0} has not been assigned in the inspector. Consider to populate or remove the entry.";
                Debug.LogWarning(string.Format(messageFormat, i));
            }
            else
            {
                _toggles[i].group = toggleGroup;
            }
        }
    }
    #endregion
}

A slightly different way would be to extend the Toggle class and expose some kind of Data property so that you can query the associated data all the time.