Search Unity

Question Whats an enum?

Discussion in 'Getting Started' started by TheKrazyDev, Mar 8, 2022.

  1. TheKrazyDev

    TheKrazyDev

    Joined:
    Sep 8, 2021
    Posts:
    107
    Hello there. So for like 3 months of been doing unity bolt and saw something about enums. Could someone dumbify this for me? Like whats an example you could use this for? Does it allow you to change the values of components? Or am I a mile off? Thanks for you time.
     
  2. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Generally they make it easier on programmers because they help explain what values mean with descriptive names. For example, without enums you might have some code like this:

    Code (csharp):
    1. private int myCharacterClass = 0;
    2. //0 - not set
    3. //1 - warrior
    4. //2 - mage
    5. //3 - thief
    6. //4 - ninja
    7.  
    8. public void SetClassMage()
    9. {
    10.     myCharacterClass = 2;
    11. }
    But then wherever you use myCharacterClass you'd need to remember what each value for it means. With enums though it can look like this:

    Code (csharp):
    1. public enum CharacterClass { NotSet, Warrior, Mage, Thief, Ninja };
    2. private CharacterClass myCharacterClass = CharacterClass.NotSet;
    3.  
    4. public void SetClassMage()
    5. {
    6.     myCharacterClass = CharacterClass.Mage;
    7. }
     
  4. TheKrazyDev

    TheKrazyDev

    Joined:
    Sep 8, 2021
    Posts:
    107
    Aah OK that makes since. Thanks!
     
    Joe-Censored likes this.