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.
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.
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.
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.
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()
{
}
}
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()
{
}
}
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:
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.
Use the Add()
method to add an item to the end of the
list.
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.
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()
{
}
}
Both arrays and lists let you store multiple values, but they have different strengths.
Add()
,
Remove()
, Clear()
, and
Count
.
If you're not sure โ start with List<T>
. Itโs easier
to use and more flexible in most cases.