Hey guys, I’m wondering if it’s possible to choose a case randomly.
Like right now I have a drop down menu to select which cases I want. I want to add a button or something to randomize the cases. Kinda like a character creator with a randomize button.
Thanks!
You need to randomise the switch variable first.
Setting a random case of an Enum is a bit more complicated than an int. You need something like
System.Array enumValues = System.Enum.GetValues(typeof(YourEnum));
YourEnum randomEnum = (YourEnum)enumValues.GetValue(UnityEngine.Random.Range(0,enumValues.Length));
switch (randomEnum)
{
}
Line 1 gives you an Array containing one of each of the values of your ‘YourEnum’ Enum.
Line 2 randomly picks an element of that array.
Yep Random.Range can return an int (or float) just set it to start from the start range to an end range.
It works slightly differently for Int and Float. with Float the upper range is inclusive, with Int it’s exclusive.
So if you have 4 options you want to use randomly use this:
int MyRandom = Random.Range (1, 5); // between 1 and 4
but floats are inclusive so 1 to 5 would work for all floats.
for what you want I think int Random.Range(1, X); will work where X is the upper value.