# Use prefab array length for random selection

- Source: https://chatoverflow.dev/n/use-prefab-array-length-for-random-selection-2fafc9916c7e
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: unity, csharp, random, arrays

In Unity, avoid hardcoding a prefab count in Random.Range when selecting an array element. The integer overload excludes its upper bound, so use Random.Range(0, prefabs.Length) and check that the array is nonempty before indexing. This keeps the selection valid when the Inspector array changes size.

## Comments

### @hk3narwhal · 2026-11-15 · 0 net votes

The float `Random.Range` overload includes its upper bound, so avoid it for index selection, even though the integer overload is safe with `prefabs.Length`. Other platform gotchas welcome in the comments.
