- Home /
Unity gets stuck using Toggles like radio buttons
I have this code for setting up game difficulty:
public void SetEasyDifficulty ()
{
gameState.difficulty = Helper.EASY_DIFFICULTY;
//easyDifficultyToggle.isOn = true;
mediumDifficultyToggle.isOn = false;
hardDifficultyToggle.isOn = false;
gameState.Save();
}
public void SetMediumDifficulty ()
{
gameState.difficulty = Helper.MEDIUM_DIFFICULTY;
easyDifficultyToggle.isOn = false;
//mediumDifficultyToggle.isOn = true;
hardDifficultyToggle.isOn = false;
gameState.Save();
}
public void SetHardDifficulty ()
{
gameState.difficulty = Helper.HARD_DIFFICULTY;
easyDifficultyToggle.isOn = false;
mediumDifficultyToggle.isOn = false;
//hardDifficultyToggle.isOn = true;
gameState.Save();
}
The reason why specific lines are commented is because when uncommented and I press one of the toggles, Unity gets stuck and I have to kill the process in task manager. On the other hand, if those lines stay commented, all works except I need to press twice, the same toggle to enable it :)
What could be the problem here?
It might help us answer the question if we see the code that is calling those 3 "Set" methods. Unity hangs like that when it enters an endless loop.
Yes I though of that. The strange thing is, in whichever framework you work there is always premade radio button component. :)
Are you using a ToggleGroup? If so, remember that pressing one toggle would cause at least one other toggle to call its function (because of state change). That means if you press the toggle button for 'medium' and the toggle for 'easy' was active, Set$$anonymous$$ediumDifficulty() AND SetEasyDifficulty() would be called. Only if you press the same toggle button the second time no other function is called. So, using a ToggleGroup could be the cause for the problem you described.
I didn't know there is something called ToggleGroup until you mentioned it. No, there is nothing in the ToggleGroup field in any of the the Toggles. Working on it now.
Answer by spinnerbox · Dec 28, 2015 at 12:26 PM
How did I solved this? There is a ToggleGroup component which can be added to an empty GameObject. So create an empty game object and press the "add component" to add UI -> Toggle Group. Once you have done that select each of your toggles and fill the Toggle Group field with this empty object. This will set up all toggles in one group and once done that, you can use the following code to find which toggle is currently enabled:
using UnityEngine;
public MyToggleClass:MonoBehaviour
{
public GameObject toggleGroupHolder;
ToggleGroup tGroup;
IEnumerator<Toggle> gameDiffEnum;
string toggleName = "";
public void SetGameDifficulty ()
{
gameDiffEnum = tGroup.ActiveToggles().GetEnumerator();
gameDiffEnum.MoveNext();
toggleName = gameDiffEnum.Current.name;
if (toggleName == Helper.EASY_DIFFICULTY_TOGGLE)
{
gameState.difficulty = Helper.EASY_DIFFICULTY;
}
if (toggleName == Helper.MEDIUM_DIFFICULTY_TOGGLE)
{
gameState.difficulty = Helper.MEDIUM_DIFFICULTY;
}
if (toggleName == Helper.HARD_DIFFICULTY_TOGGLE)
{
gameState.difficulty = Helper.HARD_DIFFICULTY;
}
gameState.Save();
}
}
Make sure to fill toggleGroupHolder with the empty object we created and set onValueChanged() event handler for each of the toggles to SetGameDifficulty(). SetGameDifficulty() will be called twice :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612053833im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Send inputfield entries via email 2 Answers
Changing UI for network chat 1 Answer
UI Click pass through 1 Answer
Toggle Button: different color for ON and OFF state? 3 Answers