Unity Community


Results 1 to 7 of 7

  1. Location
    Vancouver
    Posts
    9

    Enums in Unity (C#)

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

    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. Location
    Port Elizabeth, South Africa
    Posts
    371

    Reply

    You could try to declare enCurrent outside of the start as a private variable as such:
    Code:  
    1. private enMenuScreen enCurrent;
    Then set it in start to enMenuScreen.Main


  3. Location
    Sweden
    Posts
    91
    "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:  
    1. case enMenuScreen.Main
    2. ...
    3. case enMenuScreen.Options


  4. Location
    Vancouver
    Posts
    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. 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. Posts
    15
    Hi, you can use .ToString() in C#

    for example you create a class with enum :


    Code:  
    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. }

    and you create a class for read Class Sample enum(string):

    Code:  
    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. }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •