---
title: "How do you get an item at an index from a List in C# in Unity?"
description: "Read a list item with the indexer list[i] in C#. Check that the index is in range to avoid ArgumentOutOfRangeException, or use ElementAtOrDefault."
url: "https://bugsee.com/answers/unity/c-sharp-list-get-item-at-index/"
---

# How do you get an item at an index from a List in C# in Unity?

Use the indexer: `list[index]`. The index must be between 0 and `list.Count - 1`, or the call throws `ArgumentOutOfRangeException`.

```csharp
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Inventory : MonoBehaviour
{
    public List<string> items = new List<string> { "Sword", "Shield", "Potion" };

    void Start()
    {
        string first = items[0];                       // "Sword"
        string last = items[items.Count - 1];          // "Potion"

        int i = 5;
        if (i >= 0 && i < items.Count)
            Debug.Log(items[i]);                       // guarded access

        string safe = items.ElementAtOrDefault(i);     // null when out of range
        int index = items.IndexOf("Shield");           // 1, or -1 when missing
    }
}
```

An empty list has no valid index, so check `Count > 0` before reading the first or last item. A list shown in the Inspector can be edited there, which means its size may differ from what your code assumes.

## Related questions

- [How do variables and the Inspector work in Unity scripts?](https://bugsee.com/answers/unity/unity-variables/)
