Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

closed as off-topic by Byte56 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?"" – Byte56
If this question can be reworded to fit the rules in the help center, please edit the question.

    
No need for if(boolean==true) . Just use if(boolean) – SanSolo Oct 26 '15 at 11:41
    
@SanSolo Right, so it's basically an alternative to boolean = true. – DubGamer87 Oct 26 '15 at 16:02
1  
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#. – Byte56 Oct 26 '15 at 18:35
up vote 4 down vote accepted

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);
share|improve this answer

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!");
}
share|improve this answer

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