0

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.

corken
  • 19
  • 6

2 Answers2

0

Random.Range(0, 5) Unity documentation, created numbers from 0,1,2,3,4

The example net fiddle here will help explain this:

Fiddle output

i:0 path:4
i:1 path:1
i:2 path:3
i:3 path:2
Fatal Error: Execution time limit was exceeded

But there is still 0. Why is 0 never inserted? Because all integer values are initialized with 0 in c#. So when your Random.Range return 0, the index is already there and the while loop will forever execute.

Init your array like this and you should run fine:

int[] _asteroidPathsArray = new int[5] {-1,-1,-1,-1,-1};
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • Because I initialize the Array with a different length everytime. I normally initialized it like before, but changed every value in the Array to -1 after. It does not work though. I can not give you any Error expression because Unity just freezes. – corken Feb 24 '21 at 03:04
  • how do you initialize the array? share code please. – Athanasios Kataras Feb 24 '21 at 06:15
  • `public int asteroidPaths = 3;` `_asteroidPathsArray = new int[asteroidPaths];` `for(int i = 0; i < _asteroidPathsArray.Length; i++)` `{` `_asteroidPathsArray[i] = -1;` `}` – corken Feb 26 '21 at 14:35
0

In addition to this answer

It looks like what you actually want are just the numbers 0 to 4 in random order.

You could just use e.g.

using System.Linq;

...

_asteroidPathsArray = Enumrable.Range(0,5).OrderBy(UnityEninge.Random.value).ToArray();

Or directly

private void Inst_Asteroid()
{
    foreach(var path in Enumrable.Range(0,5).OrderBy(UnityEninge.Random.value))
    {
        var statePos = _spaceship._statePos[path];
        var asteroidPos = new Vector3(statePos.x, statePos.y, 1000);
        Instantiate(prefabAsteroid, asteroidPos, Quaternion.identity);
    }
}

Note though there is absolutely no use at all to randomize here .. since you anyway iterate over the entire array in one single go ... why not simply use them in order?

To me it doesn't look like it would make any difference:

private void Inst_Asteroid()
{
    for(var path = 0; path < 5; path++;)
    {
        var statePos = _spaceship._statePos[path];
        var asteroidPos = new Vector3(statePos.x, statePos.y, 1000);
        Instantiate(prefabAsteroid, asteroidPos, Quaternion.identity);
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115