Can you randomly choose a Boolean's value in Unity C#. I tried random.range, but it only works with floats. Here is an example of what I'm trying to do:

Boolean = (random boolean value command)(false, true);
if(Boolean = true){
    Debug.Log("It's true!")
} else{
    Debug.Log("It's false!")

Any ideas?

  • \$\begingroup\$ No need for if(boolean==true) . Just use if(boolean) \$\endgroup\$ – SanSolo Oct 26 '15 at 11:41
  • \$\begingroup\$ @SanSolo Right, so it's basically an alternative to boolean = true. \$\endgroup\$ – DubGamer87 Oct 26 '15 at 16:02
  • 1
    \$\begingroup\$ Keep in mind that a vast majority of your C# questions are not specific to game development. Unity doesn't use a special version of C#. \$\endgroup\$ – MichaelHouse Oct 26 '15 at 18:35

SanSolo answer is good and he get my +1.

Using unity Random.value here is a one line command:

bool Boolean  = (Random.value > 0.5f);

Random.Range will return an integer if both min and max are integers. Random.Range(0,2) will return either one or zero. You could use it like this:

Boolean boolValue = (Random.Range(0, 2) == 0);
if(boolValue == true){
    Debug.Log("It's true!");
} else{
    Debug.Log("It's false!");

Not the answer you're looking for? Browse other questions tagged or ask your own question.