6
\$\begingroup\$

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?

\$\endgroup\$

closed as off-topic by MichaelHouse Oct 26 '15 at 18:34

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions that aren't specific to game development are off-topic here, but can be asked on Stack Overflow. A good rule of thumb is to ask yourself "would a professional game developer give me a better/different/more specific answer to this question than other programmers?"" – MichaelHouse
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\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
7
\$\begingroup\$

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);
\$\endgroup\$
5
\$\begingroup\$

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!");
}
\$\endgroup\$

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