i could not think of any better caption. I basically want to add random Numbers from 0 to 4 in an Array of a specific Length, min 1, max 5, which is in the first for loop. The second for loop instantiates some objects which works fine. So what i did was this:
private void Inst_Asteroid()
{
for (int i = 0; i < _asteroidPathsArray.Length; i++)
{
int path = Random.Range(0, 5);
while(System.Array.IndexOf(_asteroidPathsArray, path) >= 0)
{
path = Random.Range(0, 5);
}
_asteroidPathsArray[i] = path;
}
foreach (int path in _asteroidPathsArray)
{
_asteroidPos = new Vector3(_spaceship._statePos[path].x, _spaceship._statePos[path].y, 1000);
Instantiate(prefabAsteroid, _asteroidPos, Quaternion.identity);
}
}
I do _asteroidPathsArray = new int[asteroidPaths];
before. When asteroidPaths
(the Array length) equals 5 it crashes. I want it to work in a range of 1 to 5. It works for 1 to 4 with this code. I can not spot my mistake.
I gladly appreciate any help and constructive criticism.