Matt Bird writes all sorts of nonsense, but he dedicates a large chunk of his time to writing game walkthroughs.
Random number generation comes up often when programming, and scriptwriting in Unity is no exception. Pulling a random item from a List happens all the time in video games, whether your game is drawing from a List of potential loot, deciding which monster to spawn, or determining which random event is going to happen next.
This article will explore how to pull a random item from a List. There are a few ways to get the job done.
Randomizing a Number
Before we get into Lists, however, it's not a bad idea to review how to get a random number. There's a simple, single-line method for getting a random number within a range:
This little code will instantly provide you with either a '0' or a '1' as a result, and it will place the result in the randomNumber variable for later use. Easy.
It's important to remember that the 'max' value - in this case, 2 - will never be randomly chosen, as the max value is considered exclusive. If you wanted to get a random number between, say, 0 and 100, you would set the max value to 101, not 100.
Read More From Levelskip
Randomizing a List
Pulling a random item from a List is essentially the same as getting a random number, though we will add a few more steps to make the process more useful. The code is below.
In this example we start by initializing a List of strings, stringList, and provide a series of values in the Inspector for testing. Then we create code that will trigger the randomization when we hit the R key and create a function that contains the code for the randomization called GetRandomItem(). This is a piece of code you will likely use a lot in certain games, so turning it into a function is recommended.
The function accepts the List and gives it the name listToRandomize. We then plug listToRandomize into a Random.Range calculation, basing the max value on the count (the size) of listToRandomize. That int is then used to grab a string from listToRandomize, and the string, printRandom, is returned at the bottom of the function. (Nothing is done with it, but you would then probably store the string in another variable, or send it to a text window for display, or whatever you have in mind for the item.)
There are three things to note in the example above:
- First, listToRandomize.Count will return every possible field in the List, unlike the max result of Random.Range. So you shouldn't have to worry about the final entry in the List not being chosen at some point. The print commands are included in the code so you can verify this for yourself.
- Second, you can substitute in any variable type you like besides string - int, GameObject, float, bool - and this will all work the same way.
- And, third, you can use basically the same process with Arrays, albeit with a few code substitutions. The code would look like this instead:
Arrays and Lists are quite similar to one another, though Lists can be expanded during runtime at the potential expense of extra memory usage. Use whichever one better suits your situation.