- Home /
Question by
Wolfdog · Apr 16, 2015 at 06:18 PM ·
unity5random.range
Random.Range always returns the minimum value
This is my script:
Instantiate (_shots[Random.Range (0, _shots.Length - 1)], invader.transform.position, Quaternion.identity);
Why do I always get the first result? There are 2 items in the list.
Comment
the answer might be that shots.length-1 is less than or equal to 0 maybe you populate it after this code.
Best Answer
Answer by tanoshimi · Apr 16, 2015 at 06:26 PM
Because Random.Range is exclusive of the max value, as documented: http://docs.unity3d.com/ScriptReference/Random.Range.html
You want:
Random.Range (0, _shots.Length)
Your answer
![](https://koobas.hobune.stream/wayback/20220613193314im_/https://answers.unity.com/themes/thub/images/avi.jpg)