Arrays
Arrays and Lists
Many games and experiences created in Unity have repeated items that need to be grouped for organization, easy access, and control.
Arrays are lists of elements that can be sorted, searched, counted, etc.
The index of an array is the location of the element in the list, starting with zero. The index of an element in an array is always an integer.
Why Use Arrays?
Imagine you had a game where you had a large pile of cubes that the player can pick up and manipulate, but you want to control each individually. Putting them all in an empty game object and controlling that parent game object will affect all of them.
You might consider making each cube its own variable:
....but this gets tedious and the code long.
Instead, use an array!
Adding the square brackets [] at the end of the variable type tells the program this is a list of this type of variable.
In the Inspector, this looks like this:
Twirl down the triangle to reveal more values:
Here, you can update how many items to store in the variable "cubes." Once you put in the number, fields will appear for objects and values:
Add your objects and values as you would for other parts of the component:
You'll notice the are a numbered list of "elements." The number for the element is the index of the array.
To access an object or value in an element, use the name of the array and the index inside square brackets []:
Remember: Arrays and lists start with zero, so Element or Index 2 is the third item in the list.
This means you can control things through iterative loops:
Common Array Functions and Properties
arrayName.Length - Provides the size of an array determined by the code, not the number of assigned indexes. Used mostly as a test value in for loops.
Since arrays start with zero, make sure for loop control values (usually i for index) are tested to be LESS THAN the array length.
Lists
Lists are similar to arrays, but have a little more flexibility with it comes to updating the objects and values within them.
Here's how you declare a list of Transforms:
This appears the same in the Inspector as arrays and the same script can access the elements of the list as arrays.
One big difference is that it doesn't use .Length. Instead, it uses .Capacity:
Common List Functions and Properties
listName.Capacity - Provides the size of a list. Used mostly as a test value in for loops.
Since arrays start with zero, make sure for loop control values (usually i for index) are tested to be LESS THAN the list capacity.
listName.Add() - Adds the object or value in the parentheses to the end of the list.
listName.Remove() - Removes the object or value in the parentheses. You can use listName[index] here to remove the proper item.
listName.Sort() - Sorts the list alphabetically.
Using Unity's Built-in Functions
Many built-in functions in Unity return arrays.
Below is an example of accessing a child object using the index.
GetComponentsInChildren<>() will return the parent object as well if the component exists in the parent object.
Helpful Tutorials
Last updated