In this video we see the instruction that allows us to generate random numbers within a certain range of values and then using that information we see how to access a random element inside an array of any type.
Specifically the instruction that allows us to generate random numbers in Unity is:
Random.Range(valueMinimum,valueMaximum);
In brackets the minimum and maximum values to be considered are sent as parameters with the particularity that the maximum value will not be included among the possibilities, for example the instruction:
Random.Range(0,2);
returns only two possible values, 0 and 1.
We can use this detail to generate integer random numbers that can be used as indexes of an array, making the maximum value of the Random.Range function the size of the array that we want to access, let's suppose that we have defined the following array:
public GameObject[] myObjects;
Then to select a random GameObject from the array we can do the following:
myObjects[Random.Range(0,myObjects.Length)]; //a random object from the array.
____________________________________________________________
LINKS
Portfolio: https://gamedevtraum.com/en/portfolio...
LinkedIn: / gamedevtraum
Downloads: https://gamedevtraum.itch.io/
Contact: [email protected]
____________________________________________________________