- Home /
Random.Range only returns lowest value
function Update() {
var speed = Random.Range(1,3);
if(speed == 1){
this.transform.Translate(Vector3(500,0,0) * Time.deltaTime);
}
if(speed == 2){
this.transform.Translate(Vector3(30,0,0) * Time.deltaTime);
}
if(speed == 3){
this.transform.Translate(Vector3(100,0,0) * Time.deltaTime);
}
}
So I've got this script . There's three spawn points and obviously the spawn blocks . This script is attached to the blocks and is meant to varie how fast they fly . The only problem is that it only ever returns as 1 . I've tested with deifferent speds other than 500 and that's all it does . Return 1 . How could I fix this so it returns 2 and 3 as well ?
Thanks, Stealth .
if you want the random number to be 1,2,3... then var speed = Random.Range(1,4);
As Anusha has stated, but to clarify. Check the Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
floats : Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive]
integers : Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive]
Try a loop from 0 to, say, 1000. Use print(Range(1, 3)); to see for yourself that is output is either 1 or 2. Then try print(Range(1, 4));
it's printing 1 ,2 and 3 but the speed isn't changing
Answer by wibble82 · Feb 17, 2014 at 02:19 PM
Hi
First up, Random.Range can work in 2 ways - either with integers or floats. If used with integers like you're doing, it returns a number from min, to max-1. So your code will only ever get a speed from 1 to 2, never 3.
However, seeing as you're getting a new random number every frame (in the update) you will over a period of time end up generating roughly the same number of 1s as 2s, so the object will appear to move at a constant speed (even though each frame its moving by a randomly different amount).
If what you want is to pick a speed when the object is first created you'll need to make the 'speed' a member of the class, and initialise it just once inside the Start function. That way each object will pick a random speed it should move at, then move a fixed amount each frame.
(edit) fyi, if you want genuinely random speeds, you might try var speed=Random.Range(30.0,500.0) to get a totally random number from 30 to 500. Then each frame translate by Vector3(speed*Time.deltaTime,0,0)
(and another edit) oh, and given you're messing with the 'physics' of the object, you probably want this code to be in the 'FixedUpdate' function, and use Time.fixedDeltaTime. FixedUpdate runs in tandem with the physics engine's update and so is a better place to put code like this that wants to move an object at a fixed rate. Won't have any obvious affect right now, but its good practice and will save you pain in the long run!
$$anonymous$$ain thing, paras 2 and 3. Pick one speed for each object and stick with it. Once you randomly pick the starting speed, there's no more random for that guy.
That's different from (and harder than!) random every frame. And takes a code rewrite (para 3.)
Thanks wibble and Owen :D Scipt works perfectly now :]!
Your answer
![](https://koobas.hobune.stream/wayback/20220613133810im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Lasers Fire over and over 2 Answers
Random.Range(..) not working 1 Answer
Non-repeating random number generator crashing unity 1 Answer
How do you get different random numbers for each object array? 1 Answer
Random.Range not working. 1 Answer