Grab non-specific enum value

Hey all! After browsing the web for a few hours I’ve decided to ask y’all!

I’m wondering if it’s possible to (as the title states) grab a non-specific enum value. In other words, if I were to select a variable in a list of 5 items. Then how could I retrieve the transform of any of the object without being specific? At the moment I’m assuming an enum 'list" can work just like a List, Dictionary, Array, etc. in regards to grabbing values/variables. Perhaps that’s not true, but if that’s the case, please tell me :stuck_out_tongue:

Some code to potentially clarify what I mean.

public enum Inventories{backpack, head, body, boots};

transform.position = Inventories.(transform value of one of the selected options)

I know I can use switch and such but I want to prevent using anymore code than I have to.

So you want to associate an enum value with a Vector3?

Well, a Dictionary could do that.

var dict = new Dictionary<Inventories, Vector3>();
dict[Inventories.backpack] = new Vector3(0f,1f,2f);
//... so on so forth

transform.position = dict[Inventories.backpack];

If your enum values are sequential, you can create an array/list where the index of each matches the value of the enum.

var arr = new Vector3[4] { new Vector3(0f, 1f, 2f), ... };

transform.position = arr[(int)Inventories.backpack];

Though honestly a switch is just fine IMO. It’s more readable and it compiles into less code actually. Since the dictionary is actually a pretty complex class type under the hood, so is List. Array not so much, but you still need to define it, and it’s prone to error if you modify the enum.

Where as just create a function:

public Vector3 LookupPosition(Inventories e)
{
    switch(e)
    {
        case Inventories.backpack:
            return new Vector3(0f, 1f, 2f);
        //... so on so forth
    }
}

The one downside to this is that it’s static. If you need dynamic values for each, then use the dict IMO.

An enum list is a list. Of enums. It’s just like a List in that it’s a List, but filled with enums.

All an enum is, is that you associate a name with a integer.

this:

public enum Inventories{backpack, head, body, boots};\

Just means:

public enum Inventories
{
    backpack = 0,
    head = 1,
    body = 2,
    boots = 3
}

You can convert to and from int.

var e = Inventories.backpack;
int i = (int)e;
i++;
e = (Inventories)i; //e is now 'head'
1 Like

Ah great. Thanks for the quick and detailed response! You made my night :slight_smile: