- Home /
Using an Enum for GUI.Toggle?
Hi!
Say this is my enum info:
public enum Difficulty
{
Easy,
Normal,
Hard
}
public Difficulty currentDifficulty;
How do I go about making 3 GUI.toggles out of this enum (where only one can be "currentDifficulty"/active)?
Something like this:
Answer by Jamora · Jul 22, 2013 at 07:09 PM
You use a SelectionGrid. Have the names taken from the Enum by Difficulty.GetNames(). Because Enums are convertable to ints and vice versa, the folllowing should work:
currentDifficulty = (Difficulty)GUILayout.SelectionGrid((int)currentDifficulty, Difficulty.GetNames(typeof(Difficulty)), 3, toggleStyle);
EDIT: the correct line of code.
Thanks for the quick response!
I got this (sort of) working:
currentDifficulty = GUILayout.SelectionGrid((int)currentDifficulty, Difficulty.GetNames(typeof(Difficulty)), 3, toggleStyle);
But I get this error:
Cannot implicitly convert type `int' to `$$anonymous$$ain$$anonymous$$enuScript.Difficulty'. An explicit conversion exists (are you missing a cast?)
Do you know how I can convert it correctly? (I am using C# by the way)
You need to typecast it by putting (Difficulty) in front of the int that throws the error. $$anonymous$$y guess for the appropriate place is
currentDifficulty = (Difficulty)GUILayout.SelectionGrid((int)currentDifficulty, Difficulty.GetNames(typeof(Difficulty)), 3, toggleStyle);
Your answer
Follow this Question
Related Questions
One variable set TRUE when another must be false. TOGGLE 3 Answers
Toggle not work in next scene 1 Answer
How can I disable toggles when 2 toggles checked? 1 Answer
Managing GUI Windows 1 Answer
Toggle GuiTexture 1 Answer