Initialized List of capacity 2 thinks that position 1 is out of range

Basically the title. Am I doing something wrong? I initialize the list at Start so that I can insert some elements at a specific place, but when the time comes to do so, the list gives me an out of range error. I’m making a card game and the enemy (which knows it’s position in the lineup of enemies) needs to know if it’s being attacked or not by checking the QueuedAttacks list at its position. I’m guessing this is a drawback of the List interface? It sees that position 0 is null, so it thinks it’s empty or something?

Anybody wise to what’s happening?

void Start () {
		
		QueuedAttacks = new List<GameObject>(number_of_monsters);

}

...

IEnumerator MoveToAttack (Transform child, Transform startingPlace, Vector3 endingPlace, float time) {
		float t = 0f;
		while (t < 1.0f){
			t += Time.deltaTime / time;
			child.transform.position = Vector3.Lerp(startingPlace.position, endingPlace, t);
			yield return new WaitForEndOfFrame();
		}
		child.GetComponent<WeaponController>().PrepareForBattle(PartyOfPlayers[0], selectedMonster);
		QueuedAttacks.Insert(current_monster, child.gameObject);
		// ^ this is where the error is //
		StartCoroutine(child.GetComponent<WeaponController>().Attack(selectedMonster));		
	}

Isn’t that constructor the one that only sets the capacity of the list, not how many elements are actually allocated for the list? If you check QueuedAttacks.Count right before your Insert, you should see that it’s 0.

Capacity doesn’t equal Count!

Capacity is just the preinitialized size of the internal array. The collection itself is still empty. You have to use Add to actually add elements to the collection.

Read the MSDN page on List.

Insert can only insert elements on existing indices or one after. If the collection is empty the only valid index is “0”. You can’t insert an element at position 1 if 0 doesn’t exist.