- Home /
How can I set the GUI box to deselect after another one is selected?
I am making a simple character customization and I have run into a problem where both boxes are selected for both classes and it will create both classes. How can I make it so that one when box is selected, it will automatically deselect the other box?
void OnGUI() {
isMageClass = GUILayout.Toggle (isMageClass, "Mage Class");
isWarriorClass = GUILayout.Toggle (isWarriorClass, "Warrior Class");
if (GUILayout.Button("Create")) {
if(isMageClass){
newPlayer.PlayerClass = new BaseMageClass();
} else if (isWarriorClass) {
newPlayer.PlayerClass = new BaseWarriorClass();
}
}
This is what I have for the character selection.
Answer by Kiwasi · Dec 13, 2014 at 07:41 AM
Are you version constrained? Upgrading to 4.6 and using the new UI tools would make this a trivial task.
Edit: If you are version constrained then you are probably better off using an Enum. Loop through the values to create each button in OnGUI. As enums can only have one value set then by definition the group will toggle naturally. It also makes things more extendible. Let me know if you want to go down this path and I will mock up some pseudo code.
If you don't $$anonymous$$d, I would like to see an example
Sure. Totally untested pseudo code.
enum CharacterClass {Wizard, Orc, Dragon, Tiger};
CharacterClass selectedCharacter;
void OnGUI {
foreach (CharacterClass value in Enum.GetValues(typeof(CharacterClass))) {
if(GUILayout.Toggle (value == selectedCharacter, value.ToString() + " Class")){
selectedCharacter = value;
}
}
}
There are probably a few errors in there. But once you get it to compile it should make a nice toggle for you.
enum ButtonPressed {Info, Stat};
ButtonPressed selectedButton;
void OnGUI() {
foreach (ButtonPressed value in enum.ButtonPressed(typeof(ButtonPressed))) {
if(GUILayout.Toggle (value == selectedButton, value.ToString() + " Class")){
selectedButton = value;
}
}
}
I changed some stuff around but the error I originally got was from "enum.ButtonPressed" saying "Unexpected Symbol 'enum'" and I am clueless because I have tried multiple solutions. The original one you gave me saying "Enum.GetValues" gave me the same error.
EDIT: I figured it out, thanks for the help.
Answer by Wizardman290 · Dec 13, 2014 at 08:06 AM
Try making a boolean of some sort, like
if(isMageClass){
YourBool = true;
}
if(isWarriorClass){
YourBool = false;
}
Then just use this to make one selected, if the other is false. Like WarriorClass is only active if YourBool is false or something, just something like that.
If this is nothing like your were asking I'm sorry, i am new to programming/coding/whatever, i am just trying to help. Sorry if I was not useful at all lol.