I want to have a series of three toggles such that only one can be active at a time.
Toggling one of them should turn off the other two.
The code works, sort of… I can toggle the second button, then the third, but I cannot go back up.
In other words, once I have clicked the second button, I can no longer click the first.
It hovers, and activates, but it stays off, and the second one stays on.
Same thing goes when you pick the third one, then the first and second aren’t working any more.
I feel like I’m missing something obvious.
var NewWalkToggle = GUI.Toggle ( Rect(10, 40, 120, 20), currentController.Walking, "WALK", customToggle);
var NewRunToggle = GUI.Toggle ( Rect(10, 60, 120, 20), currentController.Running, "RUN", customToggle);
var NewJumpToggle = GUI.Toggle ( Rect(10, 80, 120, 20), currentController.Jumping, "JUMP", customToggle);
if (NewWalkToggle != currentController.Walking)
{
currentController.Walking = true;
currentController.Running = false;
currentController.Jumping = false;
}
if (NewRunToggle != currentController.Running)
{
currentController.Walking = false;
currentController.Running = true;
currentController.Jumping = false;
}
if (NewJumpToggle != currentController.Jumping)
{
currentController.Walking = false;
currentController.Running = false;
currentController.Jumping = true;
}
If anyone else is searching for a better solution here is one of them using GUI
static string[] options = new string[] { "Option 0", "Option 1", "Option 2" };
static Rect position = new Rect(10, 10, 400, 100);
int selected = 2;
void OnGUI()
{
selected = GUI.SelectionGrid(position, selected, options, options.Length, GUI.skin.toggle);
}
or at UnityEditor
:
int selected = 2;
static string[] options = new string[] { "Option 0", "Option 1", "Option 2" };
void OnGUI()
{
selected = GUILayout.SelectionGrid(selected, options, options.Length, EditorStyles.radioButton);
}
Cheers.
Nevermind, I got it working by setting the New???Toggle to the same values I am setting the currentController.??? to.
Like this:
var NewWalkToggle = GUI.Toggle ( Rect(10, 40, 120, 20), currentController.Walking, "WALK", customToggle);
var NewRunToggle = GUI.Toggle ( Rect(10, 60, 120, 20), currentController.Running, "RUN", customToggle);
var NewJumpToggle = GUI.Toggle ( Rect(10, 80, 120, 20), currentController.Jumping, "JUMP", customToggle);
if (NewWalkToggle != currentController.Walking)
{
NewWalkToggle = true;
currentController.Walking = true;
NewRunToggle = false;
currentController.Running = false;
NewJumpToggle = false;
currentController.Jumping = false;
}
if (NewRunToggle != currentController.Running)
{
NewWalkToggle = false;
currentController.Walking = false;
NewRunToggle = true;
currentController.Running = true;
NewJumpToggle = false;
currentController.Jumping = false;
}
if (NewJumpToggle != currentController.Jumping)
{
NewWalkToggle = false;
currentController.Walking = false;
NewRunToggle = false;
currentController.Running = false;
NewJumpToggle = true;
currentController.Jumping = true;
}
I guess what was happening was that in the earlier if statements, it was setting the values to be out of sync…
Check this for multiple toggle radio buttons in c#
//Member variables
private bool curropt1=true;
private bool curropt2=false;
private bool curropt3=false;
In GUI
if(GUI.Toggle(new Rect(Screen.width/2+360,Screen.height-600,100,20),
curropt1, new GUIContent("Option1")))
{
curropt1=true;
curropt2=false;
curropt3=false;
}
if(GUI.Toggle(new Rect(Screen.width/2+360,Screen.height-570,100,20),
curropt2, new GUIContent("Option2")))
{
curropt2=true;
curropt1=false;
curropt3=false;
}
if(GUI.Toggle(new Rect(Screen.width/2+360,Screen.height-540,100,20),
curropt3, new GUIContent("Option3")))
{
curropt3=true;
curropt1=false;
curropt2=false;
}