Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to access enum values through their numbers?

Discussion in 'Scripting' started by RayDawg, Dec 12, 2014.

  1. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    I have a script that contains an enum with 5 values. I have another script that inherits from the first script and randomly generates a number that will represent the first script's enum contents. The thing is, I don't want to copy+paste 5 if statements to check what is the value so I thought about using a for loop. Also, I do plan on adding more values to the enum in the future which is why I want to be prepared for it. Here's what I'm talking about:

    Code (CSharp):
    1. // First script example enum
    2.     public enum ItemTypes
    3.     {
    4.                 COIN,
    5.                 SHARD,
    6.                 CRYSTAL,
    7.                 IDOL,
    8.                 RING
    9.     }
    10.  
    Code (CSharp):
    1. // Second script randomly selecting a number and assigning the enum value based on selected number
    2.  
    3. private void PickItem()
    4.     {
    5.         int randomItem = Random.Range (1, 6);     // Picks an item at random
    6.         for(int i = 1; i < 6; i++)
    7.         {
    8.             if(randomItem == i)
    9.             {
    10.                 // setItem is a function in the first script that sets the item
    11.                 // newItems is declared in the second script (the one containing the code here)
    12.                 newItems.setItem = first script.ItemTypes(i); // This is the problem
    13.             }
    14.         }
    15.  
    16. }
    Can someone please explain what I'm missing?
     
    hernandosalom likes this.
  2. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Not 100%, but seems like you're missing the fact that enums are convertible to and from int. Just go (int)ItemTypes.COIN or (ItemTypes)0.
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Also, looks like you are working with 1-based arrays
    should really be Random.Range(0,5) and i=0; i<5
     
  4. RayDawg

    RayDawg

    Joined:
    Mar 19, 2013
    Posts:
    108
    It works, thank you!
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Just wanted to mention a common technique to avoid maintaining hard-coded numbers:
    Code (csharp):
    1. public enum ItemTypes
    2. {
    3.   COIN,
    4.   SHARD,
    5.   CRYSTAL,
    6.   IDOL,
    7.   RING,
    8.   Max    //<-- not a real value; marks the end of the enum.
    9. }
    10.  
    11. private void PickItem()
    12. {
    13.   ItemTypes randomItem = (ItemTypes) Random.Range (0, (int) ItemTypes.Max);  // Picks an item at random
    14.   ...
    15.   // Or, to iterate through all types:
    16.   for (int i = 0; i < (int) ItemTypes.Max; i++) { ... }
    17. }
    This lets you easily add more item types without having to update the number in other areas of your code.
     
    image28 likes this.
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Of course, that wont work if you are assigning values to the enum yourself
    Code (CSharp):
    1.     enum things { first = 10, second = 46, third = 99, Max }
    2.  
    3.         for ( int i = 0; i < (int) things.Max; i++ ) {
    4.             Debug.Log( (things) i );
    5. //Prints numbers all the way up to 99, substituting for the names as it can
    6.         }
     
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,523
    Then don't do that. ;)

    At the risk of overwhelming this thread with excessive complexity, one can also get the enum values using Enum.GetValues(). But each technique is a choice, and I prefer the simplicity of the first way.
     
    Epictickle likes this.
  8. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    I try to avoid casting whenever possible.. Purely a preference thing, though. +1 for Enum.GetValues, .ToString, and everything else that helps me avoid casting. xP
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,498
    There is nothing wrong with casting as long as you are aware of what you are doing and why you are doing it. :)
     
  10. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Right, as long as you're not boxing/unboxing, you're good to go!
     
  11. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Oh, I know. There is no way to completely avoid casting.. It's just a personal preference thing to me.