Random.Range always returning 1!!!

Every time I run:

var number1 = Random.Range(1.0, 5.0);

it always returns 1!!!
I have never set Random.seed so I don’t know what is going on.

Here is the full code (JavaScript):

var number1 = Random.Range(1.0, 5.0);
var question1 = "";
var customGuiStyle : GUIStyle;

function Start() 
{
	if(number1 == 1.0) 
	{
		question1 = "The symbol for Carbon is...";
	}
	if(number1 == 2.0) 
	{
		question1 = "The Atomic Number for Helium is...";
	}
	if(number1 == 3.0) 
	{
		question1 = "The Atomic Number for Nitrogen is...";
	}
	if(number1 == 4.0) 
	{
		question1 = "How many Nobel Gases are there not considering Ununoctium?";
	}
}

function Update()
{
    if(number1 == 1.0)
    {
    	if(Input.GetKey(KeyCode.C))
    	{
    		Destroy(gameObject);
    	}
    }
    if(number1 == 2.0)
    {
    	if(Input.GetKey(KeyCode.Alpha2) || Input.GetKey(KeyCode.Keypad2))
    	{
    		Destroy(gameObject);
    	}
    }
    if(number1 == 3.0)
    {
    	if(Input.GetKey(KeyCode.Alpha7) || Input.GetKey(KeyCode.Keypad7))
    	{
    		Destroy(gameObject);
    	}
    }
    if(number1 == 4.0)
    {
    	if(Input.GetKey(KeyCode.Alpha6) || Input.GetKey(KeyCode.Keypad6))
    	{
    		Destroy(gameObject);
    	}
    }
}

function OnGUI()
{
	GUI.Box(new Rect(Screen.width / 2, 200, 50, 20), question1, customGuiStyle);
}

Hai try changing

var number1 = Random.Range(1.0, 5.0);

to

   var number1 = 0;

then in function start add

number1 = Random.Range(1.0, 5.0);

Hopefully this helps,

  • Jabez

You need the int version of Random.Range, and you should also be assigning it in Start (or Awake), not at class level. Also, always type your variables:

var number1 : int;

function Start() {

  number1 = Random.Range(1, 5);

  ...
}