Random.Range

I’ve been trying to create an OR statement using several variables, such as bools, floats, ints, but the console always neglects it. So I’ve narrowed it down to Setting a Float to a Random.Range, however, although this code runs through the console, there is virtually no effect. Can someone tell me what’s going wrong?

using UnityEngine;
using System.Collections;

public class RandomCardBack : MonoBehaviour 
{
	public Material first;
	public Material second;
	
	private float turn;
	
	void Awake()
	{
		turn = Random.Range(1, 1.1f);
	}
	
	void Update()
	{
		if (turn == 1)
		{
			gameObject.renderer.material = first;
		}
		
		if (turn == 1.1f)
		{
			gameObject.renderer.material = second;
		}
	}
}

Random.Range(1,1.1f) will return a float between 1 and 1.1 inclusive, so most of the time both your if statements will be false as the result will rarely be exactly those 2 numbers. There are lots of floats between those 2 values!

Use the int version instead, but with the int version the max value is exclusive so you need it to be one higher than the largest value you want, like:
Random.Range(0,2)
And have your if statement test for 0 or 1

Thanks for explaining that to me! I found your insight very helpful and I’ve gained a bit more knowledge from you. Thanks again.

For future reference ‘Random.value’ returns a float between 0 and 1.

This is useful if one wants a series of fractions - e.g. 30% of the time be red, 30% of the time be blue, 40% of the time be green.

Then you can do something as simple as:

var val = Random.value;
if (val < 0.3f)
//Red
else if (val<0.6f)
//Blue
else
//Green