Arrays are great for working with multiple variables of the same type. This is one of the tools that can greatly improve efficiency to your program.

Here is an example of what not to do.

Why would you not want to do this? Because what happens if there are 50 names? Are you going to type this out 50 times. Of course not at least I would hope you would look for a better way. And there is! Just by entering.

• public string[] names = new string[3];

or

  • public string[] names;

where you can enter how large you want your array to be.

  • It is important to save often incase of possible crashes.

This is easily expandable, by changing the index number from [3] to [50]. Then by going into the inspector in Unity the user can add any name they would like.

Another option is to call your array like this

  • public int[] ages = new int[] { 27, 6, 5, 3, 65 };

To print out a specific iteration of your array you can run the following code

Next I went on to work on an array challenge where I had to create 3 arrays one with the name of 5 kids, the age of the kids, and the cars they drove, and I was to collect the data from the last kid of each array.

Expanding upon this challenge I will now be selecting a person at random and the other arrays that associate with that person must be selected as well.

Now we will be using for loops and arrays at the same time. Having both a list of item IDs and item names we put them together creating two arrays. Each array is associated with each item. To show what item goes with the ID the following code was used.

Now we want to see if an item exists by searching for a specific name and if there is an item ID associated with it.

Time to to try a new loop and implement the Foreach loop. This also prints out the array with this new loop.

To select a specific item follow this code

Now we are going to change the colors of 3 cubes to another material color. We put the cubes in the array and from their access their mesh renderer.

Arrays are super useful and will be important for adding multiple items into games. Great for inventory, people, or multiple assets at once.

Unity Developer