- Home /
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;
}
}
}
Answer by Sisso · Dec 11, 2013 at 01:54 PM
http://docs.unity3d.com/Documentation/ScriptReference/Random.html
http://msdn.microsoft.com/en-us/library/system.enum.getvalues(v=vs.110).aspx
Yeah I got that far but my code still gives me errors for trying to explicitly use the Random.Range for the enum CubeTasks. I just want to know the way to use Random.range on the enum list
Answer by TonyLi · Dec 11, 2013 at 02:02 PM
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.
Good find but it's still giving me errors.
Error 2 Argument 1: cannot convert from 'myTestClass.$$anonymous$$yTasks' to 'int'
Error 3 Argument 2: cannot convert from 'myTestClass.$$anonymous$$yTasks' to 'int'
Error 1 The best overloaded method match for 'UnityEngine.Random.Range(int, int)' has some invalid arguments
I don't think my issue is as complex as it is in the forum.
Try casting to an int:
$$anonymous$$yTasks task = ($$anonymous$$yTasks) Random.Range(0, (int)$$anonymous$$yTasks.Count);
This only works (casting to an int) if the enumerations do not explicitly set their values for example:
enum test { none=0, two=2, four=4, six=6 }
If random.range returns 1,3, or 5; it is invalid. The helper code posted with the singleton is a good way to go when that is the case.
Answer by Hyrne · Dec 11, 2013 at 04:21 PM
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();