0

This question was asked before in a similar way, but not it is not what I am trying to achieve.

My goal:

I am working with Unity 2019.4.15f1 LTS and I am trying to spawn some obstacles as Game Objects (rocks) in a Vector2 matrix with positions, but at the same time I am also spawning the object player needs to collect. I am trying to create an algorithm so there are not bad spawns such as 3 obstacles in a raw or obstacles with a single space inside them, or even the collectibles behind objects.

EDIT: In the code I am posting, the only thing I am trying to achieve is to avoid spawning 2 rocks with an empty space between them. I only want them to be aligned or distant

My system:

The code down here checks if a cell of the matrix generated from a Random.Range is empty and also matching my requirements and if so it spawns the obstacles and collectibles.

 for(int i = 0; i < rocksToSpawn; i++)
    {
        do
        {
            RandomCol = Random.Range(0, nColoumns);
            RandomRaw = Random.Range(0, nRaws);

        } while (fullCells[RandomCol, RandomRaw] == true|| FullCells[RandomCol+ 2, RandomRaw] == true || fullCells[RandomCol- 2, RandomRaw] == true || fullCells[RandomCol, RandomRaw+ 2] == true || fullCells[RandomCol, RandomRaw-2] == true);          //it will stay in the loop until it finds empy cell or the cell I want for the algorithm
        
        Instantiate(rock, new Vector3(RandomCol+ movingGrid.position.x, -0.5f, RandomRaw+ movingGrid.position.z), Quaternion.identity, movingGrid);
        fullCells[ColR, RigR] = true;                       //true values if cell has object
    }

    //here there is another part of the code that is the same and spawns collectibles

It is working fine without my "Algorithm" , rocks and collectibles spawn only in empty cells and it is like a charm. But when I run the code with the new algorithm...

The problem:

I get an IndexOutOfRangeException: Index was outside the bounds of the array. the 90% of the times I start my code because he is searching for cells in the matrix like -1 or -2 or even higher than the lenght of the array because of the RandomCol + 2. I was thinking of adding another loop that checks if RandomCol + 2 != null but it is just too inefficient and I know I am missing something so maybe you can help me in some ways.

Thanks for everything.

Leoverload
  • 1,194
  • 3
  • 8
  • 21
  • 1
    `How to avoid having IndexOutOfRangeException` -> By not trying to access an index that doesn't exist. – derHugo Dec 12 '20 at 16:34
  • @derHugo yeah, but I want to check if there is a cell with an obstacle in the position RandomCol + 2 and -2 and eventually I will always find null positions. I wanted to achieve this without exiting the array! – Leoverload Dec 12 '20 at 16:36
  • Then make sure to clamp your values to valid indices? – derHugo Dec 12 '20 at 16:41
  • my random values are inside the bounds of the array, when I search for empty cells I find values outside the bounds, an I wanted to find a simple solution for algorithms of these types inside arrays @derHugo – Leoverload Dec 12 '20 at 16:47

1 Answers1

0

I don't understand completely why you want to add 2 or substract 2 from your random number, but when you choose a random number of minimum 0, but then substract 2, then you are ofcourse likely to end up with a negative index.

I dont completely understand your system, but would it not be viable to use the Random.Range like this:

Random.Range(2, nColoumns - 2);

so that even if you substract or add 2, the value would still be within the allowed range

Malthe
  • 75
  • 6
  • I have edited my question to be more clear. This does not solve the problem, it's a dirty fix that won't work for everything I want to implement for the algorithm – Leoverload Dec 12 '20 at 16:35
  • Ok so you want atleast 2 spaces between every spawned object? You could just create every index that you want to access during the loop: RandomCol = Random.Range(0, nColoumns); RandomRaw = Random.Range(0, nRaws); indexHigh = randomCol+2 indexLow = randomCol-2 if(indexHigh >= arraySize) { indexHigh = arraySize - 1} if(indexLow < 0) {indexLow = 0} then use the indexHigh and indexLow variables rather than the expression in your while condition – Malthe Dec 15 '20 at 08:26
  • ahh i cant format it in a comment and someone has closed your question, so i cant post it as an answer – Malthe Dec 15 '20 at 08:29
  • Thanks, I get it @Malthe , I will try this – Leoverload Dec 15 '20 at 08:49