- Home /
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index
I currently working thought this guys youtube tutorial series "Lets clone a Pokemon Game"Tutorial 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[i].rarity == Rarity.rare && AllMonsters[i].regionLocated == other.region)
{
tempMonsters.Add(AllMonsters[i]);
}
}
}
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];
}
Please post the error message and/or indicate what line is generating the error.
The error message is the title of this post, thats all it shows. After using comments I isolated the problem down to this line at the bottom.
enemy$$anonymous$$onster = temp$$anonymous$$onsters[newRandom];
Try to debug temp$$anonymous$$onsters.Count and newRandom
Debug.Log(temp$$anonymous$$onsters.Count());
var newRandom = Random.Range(0, temp$$anonymous$$onsters.Count());
Debug.Log(newRandom);
For
Debug.Log(temp$$anonymous$$onsters.Count());
I just get a value of zero every time. And the other Debug for newRandom doesn't even get called.
Answer by zharik86 · Feb 09, 2014 at 10:36 PM
@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.
You were right there were no elements in the list, because my dumb self for got that I changed the region variable from and int to a string and never updated that in the inspector.
thanks everyone for your time.
Answer by giano574 · Feb 09, 2014 at 09:19 PM
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);
Oh, I seem to be mistaken. According to the documentation Random.Range will never return the max value. http://docs.unity3d.com/Documentation/ScriptReference/Random.Range.html
You should try debugging the temp$$anonymous$$onsters.Count and newRandom, like the others say.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
BCE0044 Error 1 Answer