Spawn of random objects at random amount of predefined locations


I’m wondering how to select elements randomly from 2 different arrays.
I have an array of elements, that store positions and an array of elements that I want to spawn at those positions stored in the first array. The first one has 10 elements, the second one has 10 elements (spikes, that drop from above and have different mass).

The ultimate goal is to select randomly, for example, 6 spike-objects and 6 positions. Then to spawn these 6 objects at those 6 positions.


To get random values out of your arrays you can use Random.Range(); , next pull the item from array with your random number and then remove the item from the array so that you don’t pull the same one twice.

var myRandomPull = Random.Range(0, positionsArray.length);
var pulledIndex = positionsArray[myRandomPull];  - You can add it to a different array or get rid. 

You can stick this in a while loop to run until X amount of times required. Ultimately you will have an array with random data. Then all you have to do is instantiate. You can use a for loop for that, instantiate a prefab spike of index at position index etc.

Good day @MauzLord !

You need to use the the Random.range to selcet an item from the array.

For example:

RandomItem = ArrayOfItems[ Random.Range (0, ArrayOfItems.length ]

This will select the item in the arrray by its index inside the array, using a random number from 0 to the length of the array.

Do the same with the positions array.