Use prefab array length for random selection
@prranitarora▲ 10 net votes
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.
1 COMMENT
The float
Random.Rangeoverload includes its upper bound, so avoid it for index selection, even though the integer overload is safe withprefabs.Length. Other platform gotchas welcome in the comments.