I am currently trying to use an enum to get the state of my UI and display buttons accordingly. I somewhat know what the problem is, but since most scripting help is in java, I've had some difficulty translating to C#. Code (csharp): using UnityEngine; using System.Collections; public class UIScript : MonoBehaviour { private enum enMenuScreen {Main, Options, Extras}; // Use this for initialization void Start () { //enMenuScreen enCurrent = enMenuScreen.Main; } void OnGUI() { switch (enCurrent) { case Main: { if(GUI.Button (new Rect(140,80,140,70), "")) { Application.LoadLevel("loadingscreen"); } if(GUI.Button (new Rect(180,80,140,70), "")) { enMenuScreen = Options; } break; } case Options: { if(GUI.Button (new Rect(140,80,200,150), "")) { } break; } } } } I assume the problem is that the enum has no initial value, so it doesn't know what to do first. The "//enMenuScreen enCurrent = enMenuScreen.Main;" is what I thought would fix it but when I try, Unity tells me that enCurrent doesn't exist. This is very basic, since I'm still prototyping. I am just stuck on this one issue.
You could try to declare enCurrent outside of the start as a private variable as such: Code (csharp): private enMenuScreen enCurrent; Then set it in start to enMenuScreen.Main
"The "//enMenuScreen enCurrent = enMenuScreen.Main;" is what I thought would fix it but when I try, Unity tells me that enCurrent doesn't exist." That's because it doesn't exist. You declared the variable in the Start-function, meaning once the Start-function is done, the variable goes out of scope = doesn't exist anymore. Move the line in the Start-function outside of it and it should work fine. You will also have to change the case-statements to Code (csharp): case enMenuScreen.Main ... case enMenuScreen.Options
Awesome, thanks so much. I'd actually made this work using an int, but having it this way will help the pipeline a lot.
this may be extremely old, but i found another error. when you want to switch via button. you need to use the current enum and assign in instead of the one that declares the enums. for example you have enMenuScreen = Options; but it should be enCurrent = enMenuScreen.Options; just incase anybody else wonders upon this post like me
Hi, you can use .ToString() in C# for example you create a class with enum : Code (csharp): using UnityEngine; using System.Collections; public class Sample : MonoBehaviour { public enum sampleEnum{ FirstSample, SecondSample, ThirdSample, } } and you create a class for read Class Sample enum(string): Code (csharp): using UnityEngine; using System.Collections; public class ReadEnum : MonoBehaviour { private Sample.sampleEnum sampleVariable; // Use this for initialization void Start () { string sampleString = sampleVariable.ToString(); switch (sampleString) { case FirstSample: { ...... } }
public enum JumpState { NormalJump, LongJump}; public JumpState jumpValue; and you can change the value like jumpValue = JumpState.NormalJump;