Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • Arrays and Lists
  • Why Use Arrays?
  • Common Array Functions and Properties
  • Lists
  • Common List Functions and Properties
  • Using Unity's Built-in Functions
  • Helpful Tutorials

Was this helpful?

  1. Coding Basics

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:

public Transform cube01;
public Transform cube02;
public Transform cube03;
public Transform cube04;
public Transform cube05;

....but this gets tedious and the code long.

Instead, use an array!

public Transform[] cubes;

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 []:

// Moving only the third listed cube forward
cubes[2].Translate(Vector3.forward);

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:

// Moving all cubes forward
for (int i = 0; i < cubes.Length; i++)
{
    cubes[i].Translate(Vector3.forward);
}

// Moving every other cube forward
for (int i = 0; i < cubes.Length; i++)
{
    if (i % 2 == 0)
    {
        cubes[i].Translate(Vector3.forward);
    }
}

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:

public List<Transform> cubes;

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:

// Moving all cubes forward
for (int i = 0; i < cubes.Capacity; i++)
{
    cubes[i].Translate(Vector3.forward);
}

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.

public Transform parentObject;

// Move the third child object upward
parentObject.GetComponentsInChildren<Transform>()[3].Translate(Vector3.up);

GetComponentsInChildren<>() will return the parent object as well if the component exists in the parent object.

Helpful Tutorials

PreviousConditionals (If / Then / Else)NextLoops

Last updated 4 years ago

Was this helpful?

Loops
Click here for a helpful tutorial by the creators of Unity about how to use lists and dictionaries within Unity