I want to save / load menu settings, and I have some problem with togglegroup.
Example:
ToggleGroup
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’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.
@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.