Unity C# - Filling enum with Array data

This is my code, I know I can just add the names to myEnum but thats not the point of what I want to do. I’m trying to keep this all public and fill from a list so I can simple select, for example, a gameobject the player will spawn holding etc etc.

string[] names = {"Matt", "Joanne", "Robert"};

public enum myEnum {names};
public myEnum[] WepSlot;

I’m new to enum. for some reason, instead of myEnum taking the whole array in names, it just takes the word (names) which is kinda funny, but not what I want. I want the myEnum to populate with the array (names), I was wondering if this is possible and if so, how?

No you cannot do this. An enum is not an array of strings or other values and needs to be specified exclusively and cannot depend on other variables. It takes the word “Names” because you have given it the word “Names” where you are supposed to declare the enum values.

You can however define all the enum values and then put all those values in an array later at runtime:

public enum MyEnum { Matt, Joanne, Robert };

public MyEnum[] myEnums;

private void Start()
{
    myEnums = (MyEnum[])System.Enum.GetValues(typeof(MyEnum));
}

myEnums will now contain a list of all the different enum values.

An enum is an enumeration of constants.

In other words, a group of values which cannot be changed at runtime.

In the context which you’ve provided, 'myEnum' is the name of your enumeration, and it contains a single constant named 'names'. Since you did not assign a value to 'myEnum.names', it defaults as an integer with a value of zero…

If you were to do the following:

public enum myEnum {names, ages};

then myEnum.names would still equal zero, but myEnum.ages would equal 1 because it is next in the list and also was never assigned a value.

You could just as equally assign values to these entries, like so:

public enum myEnum {names=10, ages=3};

and just as you would expect, myEnum.names would be 10, and myEnum.ages would be 3.

This is a very useful tool for making constant values more human-readable, and organizing your constants in a syntactically flavored manner. Once you have a full understanding of them, I’d recommend looking into using them as bit flags for advanced bit-masking techniques.

Simply put enums can’t be changed at runtime. You are stick with the values you declare in code.

Are you coming from JavaScript? Me too. Here’s the deal. Enums are unique, special little things. Nonexistant in JS as far as I can tell. They’re like… no they are… a set of constants. You can’t change them. Don’t bother with that. There are other more C# ways to do what you’re wanting to do.

So let’s get down to what you actually want to do, then. In JS, there’s this:

var statsX = {
    "Meghan": 1,
    "John": 1,
    "Blargh": 1
}
var statsY = {
   "Meghan": 2,
   "John": 2,
   "Blargh": 2
}

Or something along these lines. And you want to use statsX[names.Meghan] = 3 or something. Arrays aren’t made the same way at all in C#. Use a dictionary object if you want to add something like statsX["Brandon"] at runtime.

Why would you make something you can’t change at runtime you ask? (you didn’t, but you haven’t accepted/rewarded the answers that do answer your direct question, and I’m still getting to this page two and a half years later, so might as well fill in some blanks here).

The purpose of enums is to allow quick access to variables such as: Directions.up, Directions.down, Directions.left, etc. You can then do stuff like if (thisDir == Directions.up) {}, and your project OOP editor (MonoDevelop, Visual Studios, or other programs) will automatically know that Directions.back exists and Directions.backwards does not exist.