We are now ready to write a clever method that will copy levelPrefab
and place it at the right location on the level.
Add the following method to the LevelGenerator
class:
When creating procedurally generated levels, we want to make sure that the level is different every time the player sees it. For our solution, we simply want to pick a random element from the levelPrefabs
.
Please take a look at Unity's reference and search for Random.Range
. You will realize that Random.Range
returns a randomly generated number that lies between two numbers (min
and max
).
So, if we use Random.Range
, passing 0
and the count of all level prefabs, we have at our disposal a random integer number. We can use this number as an index from the levelPrefabs
list. This is exactly what we are doing in line 33.
Great! Now we know how to generate a random number to help us pick the right levelPrefab
to copy and add to the level.