Set a Toggle active in ToggleGroup via script
I want to save / load menu settings, and I have some problem with togglegroup.
Example:
ToggleGroup - Toggle1 - Toggle2 - Toggle3
When user saves the settings I save the name of the active toggle in a serializable, where I have: string toggleName = Toggle2; for example.
By default the togglegroup shows the default active toggle, but I want it to change programatically (i prefer c#) from the loaded toggleName string, but the ToggleGroup seems to lack this feature.
So is there some best practice to do it, maybe my approach is wrong?
Thanks
I am having exactly the same issue and can't see an obvious solution
Great, I now have the same problem, but as it seems there is no simple solution to find for this bs.
Answer by Farresto · Mar 28, 2017 at 08:50 PM
I've run into the exact same problem today.
I have a list of user profiles for player to select (so there's only 1 single active profile at any given time), but when you return to the profile selection screen, I wanted the selected profile to be already selected/highlighted.
There's no elegant solution I could think of, but what I did was something like:
// Set an int instead of a string whenever I save the current selected profile:
PlayerPrefs.SetInt("selectedProfile",0);
// And when loading the scene:
private int selectedProfile;
selectedProfile = PlayerPrefs.GetInt("selectedProfile");
// Then I find the parent that will have the list of profiles (you can use any other method here, it depends on how you assemble the list):
ContentSizeFitter rowheader = FindObjectOfType<ContentSizeFitter>();
// And then, because my list creation always follows the same pattern, the int selectedProfile will always be equal to the child created (and if not, you can just add a +1 or whatever):
rowheader.transform.GetChild(selectedProfile).GetChild(0).GetComponent<Toggle>().isOn = true;
There might be a proper "best practice" here, but I needed to get this done super fast and move on, so this will be it.
It's been a year since you posted this, but seeing that there's no answer yet, perhaps this might help the next guy looking for it.
Answer by unity_rhELxLN6lqNVIw · Apr 13, 2021 at 12:11 PM
@Sinusgamma I'm not sure that this would achieve what you want.. but if you have a number of toggles assigned to a toggle group, setting one of the to on would set the other ones automatically to off. All what you need to do is to set the toggle you want to on ToggleName.isOn = true and the others will be turned off automatically. Hope this helps.
Your answer
![](https://koobas.hobune.stream/wayback/20220612073025im_/https://answers.unity.com/themes/thub/images/avi.jpg)