- Home /
Random.Range Generating Assigned Value
Everytime I play my scene, I only get 1 as the generated number. When I change the value in the inspector, it sticks as that. Any help is appreciated. Thanks as always.
var number : float;
function Update () {
number = Random.Range (1,2);
Debug.Log (number);
if (number >= 1) {
animation.Play("spikeyMoveJump");
}
else {
animation.Play("spikeyMoveFall");
}
}
Answer by AlucardJay · Jul 08, 2013 at 05:00 PM
http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
Description
Returns a random integer number between min [inclusive] and max [exclusive] (Read Only).
The key point here is the maximum value is exclusive.
So your value will always be 1. If you want 1 or 2, use
number = Random.Range( 1, 3 );
The function was designed to work on arrays: Random.Range(0, array.Length). array[array.Length] is out of range.
Ah, got it. Sorry, I didn't RTF$$anonymous$$, I just assembled knowledge from forum posts. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613105712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Randomly generated number 0 Answers
Adding GUI to this 1 Answer
Lottery system 1 Answer
Generate a random number that is different every time? 1 Answer
Adding GUI to this 1 Answer