How to access enum values through their numbers?

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:

// First script example enum
    public enum ItemTypes
    {
                COIN,
                SHARD,
                CRYSTAL,
                IDOL,
                RING
    }
// Second script randomly selecting a number and assigning the enum value based on selected number

private void PickItem()
    {
        int randomItem = Random.Range (1, 6);     // Picks an item at random
        for(int i = 1; i < 6; i++)
        {
            if(randomItem == i)
            {
                // setItem is a function in the first script that sets the item
                // newItems is declared in the second script (the one containing the code here)
                newItems.setItem = first script.ItemTypes(i); // This is the problem
            }
        }

}

Can someone please explain what I’m missing?

1 Like

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.

4 Likes

Also, looks like you are working with 1-based arrays
should really be Random.Range(0,5) and i=0; i<5

It works, thank you!

Just wanted to mention a common technique to avoid maintaining hard-coded numbers:

public enum ItemTypes
{
  COIN,
  SHARD,
  CRYSTAL,
  IDOL,
  RING,
  Max    //<-- not a real value; marks the end of the enum.
}

private void PickItem()
{
  ItemTypes randomItem = (ItemTypes) Random.Range (0, (int) ItemTypes.Max);  // Picks an item at random
  ...
  // Or, to iterate through all types:
  for (int i = 0; i < (int) ItemTypes.Max; i++) { ... }
}

This lets you easily add more item types without having to update the number in other areas of your code.

1 Like

Of course, that wont work if you are assigning values to the enum yourself

    enum things { first = 10, second = 46, third = 99, Max }

        for ( int i = 0; i < (int) things.Max; i++ ) {
            Debug.Log( (things) i );
//Prints numbers all the way up to 99, substituting for the names as it can
        }

Then don’t do that. :wink:

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.

1 Like

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

There is nothing wrong with casting as long as you are aware of what you are doing and why you are doing it. :slight_smile:

Right, as long as you’re not boxing/unboxing, you’re good to go!

Oh, I know. There is no way to completely avoid casting… It’s just a personal preference thing to me.