- Home /
Is it possible to choose a random case (Switch)
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!
Depending on the type of your cases, it depends on how you would do it. If you are using Enum, C# has some ways in getting that info and you can put them in a list and randomize a number between 0 and the length - 1.
Oh sorry haha I guess I did not realize there's more than one switch statements. I'm using Enums.
Answer by Bonfire-Boy · Feb 07, 2015 at 09:45 PM
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.
for add, you have to insert this code into awake() or start()
No, you don't.
The code I've shown will pick a random enum and then perform a switch over it, which is what was asked for.
You might want to do that in Awake() or Start(), if you're using it to initialise something.
But there are many other times one might want to randomise something including in response to a request to do it (which is what the OP actually asked for).
Answer by Mmmpies · Feb 07, 2015 at 09:25 PM
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.
Sorry I'm quite new to program$$anonymous$$g, so that answers the Random generator part. How would I go about making the switch statement be affected by the $$anonymous$$yRandom number?
Ok that definitely looks complicated for me. I take it I need to declare an Array before this code? When I insert it anywhere in my script, the Array is red, kinda like when it can't find the ref to a variable.
Sorry, my bad, I left some pseudo in there. Have fixed it now. Array
should have been YourEnum[]
(an array of your enums). The GetValues
function sets it so that it contains one of each possible value of that enum, and then a random case is picked from that array.
$$anonymous$$y double bad, I think you just didn't have the System namespace loaded. Have fixed it again adding in explicit namespaces.
Haha, that's a relief! Ok so I put my enum in the but now the Enum.GetValues is red. everything else seems ok. I put all of them in Start() for now should I not have done that?
Your answer
Follow this Question
Related Questions
Recoil in Z axis 0 Answers
how do you make a huge 2d texture map made of random picked textures? 2 Answers
Spawning villages on random gen Terrain 1 Answer
Creating new scriptable objects at runtime 1 Answer
Objects will not spawn in a 2d game. 0 Answers