ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

I currently working thought this guys youtube tutorial series “Lets clone a Pokemon Game”[Tutorial][1] and I keep getting this “ArgumentOutOfRangeException: Argument is out of range. Parameter name: index” And for the life of me I can’t figure out why? As far as I can tell my code looks just like his.

#pragma strict

import System.Collections.Generic;

//All Enemies

var AllMonsters : monster[];

var enemyMonster : monster;

var monsterEquipped : monster;

function Start () {

}

function OnGUI ()
{

	var other : Player_stats;
	other = gameObject.Find("_Player").GetComponent(Player_stats);
	
	if(other.isInCombat)
	{
		GUI.Label (Rect(50, 100, 200, 100), ""+enemyMonster.name);
		GUI.DrawTexture(Rect(70, 100, 128, 128), enemyMonster.image);
	}
	
}

function Update () 
{
	
}

function randomizeMonster ()
{
	var other : Player_stats;
	other = gameObject.Find("_Player").GetComponent(Player_stats);
	
	var tempMonsters : List.<monster> = new List. <monster>();
	var randomNum : int = Random.Range(0,100);
	
	if(randomNum == 20)
	{
		for(var i = 0; i < AllMonsters.Length; i++)
		{
			if (AllMonsters_.rarity == Rarity.rare && AllMonsters*.regionLocated == other.region)*_

* {*
_ tempMonsters.Add(AllMonsters*);
}
}
}
else*
* {
for(var j = 0; j < AllMonsters.Length;j++)
{
if(AllMonsters[j].rarity == Rarity.common && AllMonsters[j].regionLocated == other.region)
{
tempMonsters.Add(AllMonsters[j]);
}
}
}*_

* var newRandom = Random.Range(0, tempMonsters.Count);*
* enemyMonster = tempMonsters[newRandom];*
}
_*[1]: Unity Lets Clone a Game [ Pokemon ] - Part 14 - YouTube

@giano574 Everything speaks correctly. Your error is that you have an empty list (just it is visible from logs). In your list there is no element. The most correct that it is possible to make, it in case of initialization to add an element, for example:

  var tempMonsters : List.<monster> = new List. <monster>();
  tempMonsters.Add(AllMonsters[0]);

Or to do any check on existence of elements in the list: tempMonsters.Count != 0.

The problem is that you are creating a random between 0 and tempMonsters.Count. If the array is say, 3 long, it will look like this:

[0] = 1
[1] = 2
[2] = 3

In this case tempMonsters.Count will return 3. If you look for something at the index 3 in tempMonsters you will get an error, because there is nothing at this index. Change your code to:

var newRandom = Random.Range(0, tempMonsters.Count - 1);

I get this error in a blank project. Why?