Randomize an enum list

I have an enum list that is going to have a different task each time I run the app. But I do not know how to set the enum list to be random. Can anyone show me how to do it? Here is my code:

using UnityEngine;
using System.Collections;

[ExecuteInEditMode]
public class myTestClass : MonoBehaviour {
public enum MyTasks { task1, task2, task3, task4, task5, task6 }
    MyTasks currentTask = MyTasks.task1;
	public Rect GUIRectWindow, r_msg;
	void Awake()
    {
        //TODO: Initialize stuff here
    }
    void OnGUI()
    {
		// Make a background box
        GUI.Box(GUIRectWindow, "");
        switch (currentCubeTask)
        {
            case CubeTasks.task1:
                drawLabel(r_msg, "This is task 1");
                break;
            case CubeTasks.task2:
                drawLabel(r_msg, "This is task 2");
                break;
            case CubeTasks.task3:
                drawLabel(r_msg, "This is task 3");
                break;
            case CubeTasks.task4:
                drawLabel(r_msg, "This is task 4");
                break;
            case CubeTasks.task5:
                drawLabel(r_msg, "This is task 5");
                break;
            case CubeTasks.task6:
                drawLabel(r_msg, "This is task 6");
                break;
        }
    }
}

Under the hood, enums are ints that start at 0 by default (although you can change this). You can cast back and forth, and use Random to select a task randomly:

public enum MyTasks { task1, task2, task3, task4, task5, task6, Count }
MyTasks task = (MyTasks) Random.Range(0, MyTasks.Count);

I added a fake enum “Count” to serve as a shortcut for the Random.Range() function.

However, if you want to shuffle a list of tasks, you’ll have to do a little more. See andeee’s forum thread for some guidance and code. In brief, you’ll fill an array with tasks 1-6 and then shuffle the order of the elements.

helper Code:


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EEnum : Singleton<EEnum>
{
	public T GetRandomEnum<T>()
	{
		System.Array A = System.Enum.GetValues(typeof(T));
		T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
		return V;
	}
}

Usage:

public Prefix 			_Prefix;  

TMP_Name._Prefix = EEnum.Instance.GetRandomEnum();