Use the indexer: list[index]. The index must be between 0 and list.Count - 1, or the call throws ArgumentOutOfRangeException.
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.
