How do I use an enum value as a variable?

I’m trying to make a weapon cycle through two modes, and in order to do this I’ve created an enum for the two modes, and tried to equal an integer to the length of the enum. However, apparently I can’t use the enum as a variable. Since I have only beginner knowledge of enums, can someone fill me in?

Code:

public enum PullModes {PullMode1, PullMode2};

	public PullModes pullModes;

	private int numberOfPullModes = System.Enum.GetValues(PullModes).Length;

When you declare an enum you actually create a new type. However every enum type has an underlying integral data type (sbyte, byte, short, ushort, int, uint, long, ulong). If no explicit type is specifed the default is “int”. So any enum value can be casted into it’s underlying type and back:

public enum MyEnum : int
{
    A, B, C, D
}

// ...
MyEnum someEnumValue;

someEnumValue = MyEnum.B;
int val = (int)someEnumValue; // val == 1
someEnumValue = (MyEnum)2; // someEnumValue == "C"

Note that an enum value literally is the same as it’s underlying value. You can even assign an integer value to an enum that doesn’t have a corresponding “constant”:

someEnumValue = (MyEnum)42; // no constant inside MyEnum for this value 42

A common way if you want a countable enum is to define a special “Count” or “Size” value as last element:

public enum Mode
{
    M1,     // == 0
    M2,     // == 1
    M3,     // == 2
    M4,     // == 3
    Count   // == 4
}

public Mode mode;

This way one can simply do:

mode++;
if (mode >= Mode.Count)
    mode = Mode.M1;

This would increase the value by one each time and checks if we reached our additional “Count” value if so go back to the first. That way you have a cycle. M1 → M2 .> M3 → M4 → M1 → M2 → …

Note that this of course only works as long as your enum values are sequencial. Enum values can be specified however you like:

public MyEnum
{
    A = 45,
    B = 545,
    D = 65536,
    chicken = 1
}

Am I correct in assuming you don’t actually want to assign the length of the enum (which in the above example will always be 2)?

If you want to switch between the two modes, this is how to do it:

    public void Start()
    {
        // Default to first pullmode
        public PullModes pullmodes = Pullmodes.PullMode1;
    }

    public void Update()
    {
        SwapPullModes();
    }
    
    private void SwapPullModes ()
    {
        // Switch between the two pull modes
        if (pullModes == PullModes.PullMode1)
        {
            pullModes = PullModes.PullMode2;   
        }
        else
        {
            pullModes = PullModes.PullMode1;
        {
    
    }

@tomandresen

This works fine. I do have one question, though: what would I do if I had more than 2 modes to the weapon?

If you want to move the enum to the next value you can use this

public static class EnumExtensions
{
    public static T Next<T>(this Enum e)
    {
        try
        {
            var enumInfo = Enum.GetValues(typeof(T));
            var currentValue = e.ToString();
            for (int a = 0; a < enumInfo.Length; a++)
            {
                if (((T)enumInfo.GetValue(a)).ToString() == currentValue.ToString())
                {
                    if (a == enumInfo.Length - 1)
                    {
                        return (T)enumInfo.GetValue(0);
                    }
                    else
                    {
                        return (T)enumInfo.GetValue(a + 1);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Log("Error while read enum: " + ex.Message);
        }
        return default(T);
    }
}

public class YourClass : MonoBehaviour
{
    public enum PullModes { PullMode1, PullMode2, PullMode3 };

    public PullModes pullModes;

    public void Awake()
    {
        for (int i = 0; i < 10; i++)
        {
            pullModes = pullModes.Next<PullModes>();
            Debug.Log(pullModes.ToString());
        }
    }
}