Problem with Random.Range

Hi everyone!
I want to create an object, but just if the random.range(0,1) is 1. If it’s 0, then nothing happens.
Thank you and sorry for my bad english!
Here is my code:

var cube : GameObject;
var spawnPosition : GameObject;
var timer = 0.0;

function SpawnCube() {

    var tempCubeSpawner = Instantiate(cube, spawnPosition.transform.position, Quaternion.identity);


function Update() {

    timer += Time.deltaTime;
    if(timer > 5) {
    var randomNumber = Random.Range(0,1);
    if(randomNumber == 0) {
      timer = 0.0;
      if(randomNumber == 1) {
      timer = 0.0;



The upper bound of Random.Range() is exclusive (i.e., it will never be selected). Thus Random.Range(0, 1) will always return 0. See the documentation for more details.

You could instead use var randomNumber = Random.Range(0.0f,1.0f) and check if randomNumber is less than 0.5f (which it should be roughly 50% of the time) instead of checking if it equals 0 or 1.

Random.Range is overloaded, if you pass 2 int variables or values (explicit numbers WITHOUT a letter at the end) it’ll return an integer random value between both values excluding the biggest one. If you pass at least one float variable or value (a number with an “f” at the end) it will return a real random value between both values, including the biggest one.

If you want your Random.Range to return 0 or 1 (and nothing in between) you should use Random.Range(0,2).

The suggestion of making the numbers float numbers and checking if they’re greater than 0.5 is bad, it’s a bad approach since it adds extra complexity to something that should be really simple, and if you for any change need a random value between 0, 1 and 2 in the future, you have to change that “is greater than 0.5” comparition since it doesn’t make sense now.

Use the correct function for the result you want. You want random integer numbers, so use the Random.Range function that returns integer values.