ASTEROIDS - ARRAY
At this point the asteroids contained all the behaviors it should have had in the game. So I created a prefab of the 3 asteroids by dragging and dropping it down to my prefabs folder in Project window. A new empty GameObject was created in the Hierarchy (goto Hierarchy --> Right-click --> Create Empty)
We have 3 different sizes and shapes of asteroids in the scene. So, an Array was used to store the collection of asteroids.
DECLARING AN ARRAY OF ASTEROIDS
When declaring an array we follow the similar procedure that we use to declare other variables. A data type is specified and a name of the variable is declared. To declare an array, the only difference is that we include a square brackets [ ] after the data type.
In the SpawnManager GameObject, a new Script "SpawnManager.cs" was added to handle the random instantiation of asteroids.
Inside SpawnManager script, an array was declared with a data type GameObject to store 3 asteroid prefabs created.
INSTANTIATE RANDOM ASTEROIDS
This section discusses on how the random (out of 3 prefabs) asteroids were instantiated at random x-position using asteroid prefabs' rotation.
RANDOM INDEX FOR ARRAY
It is necessary to create a random index for an array so that every time the Game Object is instantiated, a random Index in fed into the code and random asteroid clone is created.
For this, Random.Range(int minInclusive, int maxExclusive) was used.
This method is used to return a random int within [minInclusive and maxExclusive]
In this case, minInclusive number = 0, and
maxExclusive number = Array.Length (Length of an array or number of elements stored in an array)
RANDOM SPAWN POSITION
The random asteroids should spawn from