This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin

What is an Array and how is it useful? An array is a type of variable that can hold multiple values for the same data type. They are useful when you have variables to reference things like names, ages, items, etc., where you could otherwise be creating hundreds of individual variables for each different value.
Each array variable here shows a different way to declare the array, purely for the sake of educational value. The first array example for names is arguably the most common way to use arrays in Unity. The size and contents of the array can all be addressed in the inspector. The main issue with taking this route, is that nothing is hard coded, so all of the values live in Unity’s inspector (save often). The second array for ages, declares an array with 5 elements. While this can be added to in the inspector, it has a similar downfall. The hard-coded value is only for 5 elements, so anything else is specifically tied to Unity’s inspector and saving your work! The final array for car models hard codes the values into the script. All of the information will be retained here in the script outside of Unity. Normally, I would need to adjust the code to change the array size and the respective values, but using Serialize Field enables me to overwrite these values in Unity’s inspector.

The left image shows how the inspector looks when opening the above script in Unity, while the image on the right shows the changes I made in the inspector. Only the last array for car models has all of the information provided, being it was hard-coded in the script.


In order to access and print these values to the console, the element number that corresponds to the desired value can be typed within some square brackets. For names[0], Ricky will be printed (element 0). Ages[3] will print 3 as element 3, and carModels[4] will print the Luxury car option.

Here is the console print-out.

As a challenge, I need to print out the last element of each array, being they associate with the same character. I use string interpolation here to insert the array names between some text, with the corresponding element numbers for what I want printed out. For bonus points, this will print out when the space key is pressed.

Here is the print out, showing all element 4 selections.

As an additional challenge, I have been tasked with randomizing the selected kid, and to print out the values associated with the corresponding element number. To do this, I create an local int variable called randomKid. This uses Random.Range between zero as a minimum, and names.length as the maximum amount value in the specified array. Names.Length will keep the maximum value the same as the number of values in the names array!

Here is a clip of the random values printing to the console when I press the space key!

Here are the printed results for my randomly featured kiddos!

In my next article, I will tackle some more advanced array techniques and work to accomplish a few more challenges. Thanks for reading!
Working with Arrays Part 1: The Basics was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin

Jared Amlin | Sciencx (2021-08-11T00:54:32+00:00) Working with Arrays Part 1: The Basics. Retrieved from https://www.scien.cx/2021/08/11/working-with-arrays-part-1-the-basics/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.