Enums in Unity (C#)

Discussion in 'Scripting' started by KWright, May 15, 2010.

  1. KWright

    KWright

    New Member

    Joined:
    May 8, 2010
    Messages:
    9
    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):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class UIScript : MonoBehaviour {
    6.  
    7.     private enum enMenuScreen {Main, Options, Extras};
    8.     // Use this for initialization
    9.     void Start () {
    10.         //enMenuScreen enCurrent = enMenuScreen.Main;
    11.     }
    12.    
    13.     void OnGUI()
    14.     {
    15.         switch (enCurrent)
    16.         {
    17.             case Main:
    18.             {
    19.                 if(GUI.Button (new Rect(140,80,140,70), ""))
    20.                 {
    21.                     Application.LoadLevel("loadingscreen");
    22.                 }
    23.                 if(GUI.Button (new Rect(180,80,140,70), ""))
    24.                 {
    25.                     enMenuScreen = Options;
    26.                 }
    27.                 break;
    28.             }
    29.             case Options:
    30.             {
    31.                 if(GUI.Button (new Rect(140,80,200,150), ""))
    32.                 {
    33.                    
    34.                 }
    35.                 break;
    36.             }
    37.         }
    38.        
    39.     }
    40. }
    41.  
    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.
     
  2. CorruptedHeart

    CorruptedHeart

    Member

    Joined:
    May 4, 2010
    Messages:
    375
    You could try to declare enCurrent outside of the start as a private variable as such:
    Code (csharp):
    1. private enMenuScreen enCurrent;
    Then set it in start to enMenuScreen.Main
     
  3. Sabo

    Sabo

    Member

    Joined:
    Nov 7, 2009
    Messages:
    92
    "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):
    1. case enMenuScreen.Main
    2. ...
    3. case enMenuScreen.Options
     
  4. KWright

    KWright

    New Member

    Joined:
    May 8, 2010
    Messages:
    9
    Awesome, thanks so much.

    I'd actually made this work using an int, but having it this way will help the pipeline a lot.
     
  5. IMTRIGGERHAPPY9

    IMTRIGGERHAPPY9

    New Member

    Joined:
    Jul 27, 2011
    Messages:
    35
    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 :)
     
  6. TheShadowblast123

    TheShadowblast123

    New Member

    Joined:
    Dec 31, 2013
    Messages:
    1
    tank you!!!
     
  7. tux83

    tux83

    Member

    Joined:
    Jan 2, 2014
    Messages:
    15
    Hi, you can use .ToString() in C#

    for example you create a class with enum :


    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sample : MonoBehaviour {
    5.  
    6.     public enum sampleEnum{
    7.        
    8.         FirstSample,
    9.         SecondSample,
    10.         ThirdSample,
    11.        
    12.     }
    13.    
    14. }
    15.  
    16.  
    and you create a class for read Class Sample enum(string):

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ReadEnum : MonoBehaviour {
    5.  
    6.     private Sample.sampleEnum sampleVariable;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.         string sampleString = sampleVariable.ToString();
    12.         switch (sampleString)
    13.  
    14.         {
    15.  
    16.             case FirstSample:
    17.  
    18.             {
    19.        
    20.             ......
    21.     }
    22.    
    23.    
    24. }