How to get a random from an array????

Hi all,

I have an array, and I need to get a random index from it. The random method from javascript does not work in Unity, so what can I use instead??

Thank you… 8)

Something like…

var randIndex : int = Mathf.RoundToInt(Random.Range(0, maxIndex));

You don’t need RoundToInt; Random.Range does integers by itself.

var randIndex = Random.Range(0, maxIndex);

(Assuming maxIndex is an int.)

–Eric

If you were instantiating a random prefab from a list of prefabs assigned in the inspector, is this the method you’d use?

Always wondered about this.

AC

Yep.

var somePrefabs : GameObject[];

function Start () {
   Instantiate(somePrefabs[Random.Range(0, somePrefabs.Length)]);
}

–Eric

Thanks Eric thats totally cool 8)

AaronC