x


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;
        }
    }


}
more ▼

asked Jul 06 '12 at 03:58 AM

darksblood gravatar image

darksblood
16 1 2 5

check out with the if statement u cannot compare gameobject with int so because of that it is displaying index out of range

Jul 06 '12 at 06:43 AM mohanrao164

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.

Jul 06 '12 at 08:00 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 06 '12 at 07:58 AM

Kryptos gravatar image

Kryptos
7.2k 5 32

oh my bad didnt check it properly, i saw the doc for float and got hasty =/.

deleted my answer.

Jul 06 '12 at 09:10 AM flamy

Thank you, I've been running the test for about 15 minutes now and it hasn't stopped the game to display the error where as before it would have done so after about a minute.

Jul 06 '12 at 12:37 PM darksblood
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x356
x65
x23

asked: Jul 06 '12 at 03:58 AM

Seen: 730 times

Last Updated: Jul 06 '12 at 12:37 PM