- Home /
Question by
PaxForce · Mar 15, 2014 at 10:46 AM ·
arrayrandomgenerationnumber
random number generator problem
This is the excerpt from my code:
IEnumerator SpawnPickUps()
{
int spawnWait = 5;
GameObject[] arPickUpsCollection = new GameObject[]{pickUp_Ignition, pickUp_Shock, pickUp_Acid, pickUp_Health};
while(true)
{
yield return new WaitForSeconds(spawnWait);
int pickUpRandomizer = Random.Range(0,3);
Instantiate (arPickUpsCollection[pickUpRandomizer], arSpawnPositions[spawnRandomizer], Quaternion.identity);
}
}
The problem is, my pickUp_Health is not instantiated. EVER. It gets instantiated only when I increase the range to (0,4) which makes no sense, because the pickUp_Health has the index of 3. What may be causing this?
Comment
Best Answer
Answer by FLASHDENMARK · Mar 15, 2014 at 11:16 AM
From the documentation:
Random.Range(min, max) : int
"Returns a random integer number between min [inclusive] and max [exclusive] (Read Only)."
Max is not inclusive, which means you will have to set 3 to 4.
Also, don't hard-code the number in Random.Range. Use Random.Range (0, arPickUpsCollection.Length)
. This is why Random.Range max is exclusive for ints, incidentally.