- Home /
Select a boolean from an array
Hi, iv made a Boolean array and filled it with 4 bools. However I don't know how to select the random bool which has been chosen. This is my code: manipsList = new bool[] {upsideDown, screenShrink, bottomRising, topFalling }; int index = Random.Range(0, manipsList.Length); Debug.Log(manipsList[index] + " has been selected");
I'm not sure how to select the random one selected. In my game one of the when one of the manipulators is chosen it alters the game world. Thanks for any help!
Answer by LiloE · Feb 22, 2017 at 12:22 PM
What you're looking for is an enum type.
enum Manips { upsideDown, screenShrink, bottomRising, topFalling }
Manips selection = (Manips)Random.Range(0, Enum.GetNames(typeof(Manips)).Length);
Debug.Print(selection .ToString());
// in the manipulation code:
switch(selection){
case Manips.upsideDown:
// do something
break;
case Manips.screenShrink:
// do something
break;
// etc...
}
Wait two seconds, see on line 2, it says that Enum does not exist in the current context? Just for clarification iv put the 1st line before the start method and the 2 in the start method..? Then the rest in update.
it misses an extra part :
public enum $$anonymous$$anips { upsideDown, screenShrink, bottomRising, topFalling }
private $$anonymous$$anips selection;
void Start ()
{
selection = ($$anonymous$$anips)Random.Range(0, Enum.GetNames(typeof($$anonymous$$anips)).Length);
}