๐Ÿ  Home

๐Ÿ“š Arrays and Lists in Unity

Sometimes a single variable isn't enough. What if we have 10 enemies at once? Or a list of fruits? Thatโ€™s when arrays and lists come to the rescue โ€” they let you store many values at once and access them by their index.

Use arrays and lists when you want to:

In this lesson, we'll learn how arrays differ from lists, and how to create, fill, and use them in Unity.

๐Ÿ”ข Arrays

An array is a variable that can hold several values of the same type. Instead of creating ten separate variables, we can group them in one array and access each value by index.

Arrays are especially useful when you know in advance how many slots you need โ€” like 5 player lives or 3 menu buttons. Each element in the array has an index, starting from zero.

Once created, the array size is fixed: you can't add or remove elements. If you need more flexibility โ€” keep reading to learn about lists.

๐Ÿงฐ Declaration and Initialization

To create an array, you specify the data type and the number of elements. You can declare it first and fill it later, or define all values immediately using curly braces.

๐ŸŽฏ Accessing Elements

Every array element has a number โ€” an index, starting from 0. You can read and change values using this index. For example, array[1] is the second element.

Below are two examples: one with numbers, one with strings. They show how to create an array, fill it, and use the values in your code.

๐Ÿงฎ Example: array of integers int[]


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // Start is called once when the object is launched
    void Start()
    {
        // Create an array of 3 numbers
        int[] numbers = new int[3];

        // Fill the array with values
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;

        // Display one of the values
        Debug.Log("Second number: " + numbers[1]);
    }

    // Update is called every frame
    void Update()
    {
        
    }
}

๐Ÿ“‹ Example: array of strings string[]


using UnityEngine;

public class CSharpScript : MonoBehaviour
{
    // Start is called once when the object is launched
    void Start()
    {
        // Array of names
        string[] names = { "Anya", "Bob", "Sonya" };

        // Display the first name
        Debug.Log("Hello, " + names[0]);
    }

    // Update is called every frame
    void Update()
    {
        
    }
}

๐Ÿ“‹ Lists

A list is like an array, but more flexible. You can add and remove elements at any time, without setting the number in advance. Lists use the type List<T>, where T is the data type (like int, string, GameObject).

Lists are commonly used when:

๐Ÿงฐ Declaration and Initialization

To create a list, use List<T> and include the namespace System.Collections.Generic. After that, you can easily add elements as your program runs.

โž• Adding Elements

Use the Add() method to add an item to the end of the list.

โž– Removing Elements

To remove an element, you can use Remove(), which finds and deletes the first match. You can also use RemoveAt(index) if you want to remove by position.

๐Ÿ”ข List Size

Instead of Length like with arrays, lists use Count to show how many elements they currently have.

Below is an example of how this works in a Unity script.


using UnityEngine;
using System.Collections.Generic;

public class CSharpScript : MonoBehaviour
{
    // Start is called once when the object is launched
    void Start()
    {
        // Create a list of strings with initial values
        List<string> names = new List<string>() { "Anya", "Bob", "Sonya" };

        // Add two more names
        names.Add("Tina");
        names.Add("Leo");

        // Remove the first name (Anya)
        names.RemoveAt(0);

        // Display the number of elements
        Debug.Log("Total names in the list: " + names.Count);
    }

    // Update is called every frame
    void Update()
    {
        
    }
}

๐Ÿค” When to use an array and when to use a list?

Both arrays and lists let you store multiple values, but they have different strengths.

If you're not sure โ€” start with List<T>. Itโ€™s easier to use and more flexible in most cases.