- Home /
Error: Argument is out of range Parameter name: index
Can someone help me understand what is going on here? Upon lauch, everything works fine and then after about a minute (sometimes longer), i get the error you see in the tittle. Please help, maybe there's something I'm not seing.
(ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) Spawnhighway.Update () (at Assets/Scripts/Spawnhighway.cs:26)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Spawnhighway : MonoBehaviour
{
public List<GameObject> Highways = new List<GameObject>();
public float waittime = 3.0f;
int highwayindex;
GameObject thehighwayspawned;
void Start()
{
highwayindex = Random.Range(1,8);
thehighwayspawned = Instantiate((Highways)[highwayindex], new Vector3(10.44449f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
}
void Update()
{
if ( thehighwayspawned.transform.position.x <= 2.368699)
{
int highwayindexx = Random.Range(1,8);
thehighwayspawned = Instantiate((Highways)[highwayindexx], new Vector3(9.686185f, transform.position.y, transform.position.z), Quaternion.identity) as GameObject;
}
}
}
check out with the if statement u cannot compare gameobject with int so because of that it is displaying index out of range
I've formatted your code for you - please indent it by 4 spaces or 1 tab before pasting, or highlight it and then press the code button after pasting.
Answer by Kryptos · Jul 06, 2012 at 07:58 AM
To properly use Random.Range, you should always use the current size of the collection (in your case the List).
highwayindex = Random.Range(0, Highways.Count);
Also make sure that your list is filled with enough values (in your case 8), before the game start.
oh my bad didnt check it properly, i saw the doc for float and got hasty =/.
deleted my answer.
Thank you, I've been running the test for about 15 $$anonymous$$utes now and it hasn't stopped the game to display the error where as before it would have done so after about a $$anonymous$$ute.