Because you didn’t explicitly set the integral type of the enum, it defaults to int. If you don’t explicity set the values of the enums, the first element in the sequence is 0 and +1 for each going down.
You can do(assuming the variable cardSuit is of type CardSuit(the enum):
One more example that once again only applies to enums that haven’t had their default values set. We know we start at 0, but perhaps we don’t want to explicity set the max, you may end up updating the enum with more elements and that would require additional maintenance. Lets remove that extra step. System.Linq is required as a using.
Use this utility method in some Utility static class:
public static T RandomEnumValue<T>()
{
var values = Enum.GetValues(typeof(T));
int random = UnityEngine.Random.Range(0, values.Length);
return (T)values.GetValue(random);
}
Then, use it in your game class like this:
private enum MyEnum
{
One,
Two,
Three
}
private void Method()
{
MyEnum e = Utility.RandomEnumValue<MyEnum>();
}