odd problem with List (argument out of range)

Hi everyone,

My unit build script gives an argument out of range exception when my unit queque list (unitName) has 2 or more units in it. For some reason it won’t give the error when I only have one unit in my unitName list.

Does anyone have any clue what the problem can be ? This is bugging me for a few days now.

Below is the code and the error message.

Thanks in advance

                void Start()
               {

        StartCoroutine ("Delayer");
               }

    }
       

       void UnitBuild()
    {
        if (unitName.Count > 0) {

            if (P1game.creditsUp1 >= unitPrice [0]) { //unit price
                P1game.creditsUp1 -= unitPrice [0];
                buildEnable = false;
            }
          }

       }




         void SpawnUnit ()
        {
               Debug.Log("spawn unit");

           SpawnBullet ();
             }

    }




        void SpawnBullet()
    {
        Debug.Log("spawn bullet");
       

        unitName.RemoveAt(0);
        unitPrice.RemoveAt(0);
        buildEnable = true;
    }



         IEnumerator Delayer()
        {
            while (true) {
            yield return new WaitForSeconds (1);
            if (buildEnable) {
                UnitBuild ();
            }
            else
                {
                SpawnUnit ();
                }

            }

the error message ;

ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[System.Int32].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
SpawnUnitG.UnitBuild () (at Assets/Scripts/GAMEscene/SpawnUnitG.cs:162)
SpawnUnitG+c__Iterator7.MoveNext () (at Assets/Scripts/GAMEscene/SpawnUnitG.cs:264)

On the line immediately preceeding the line you get the error on, use Debug.Log() to output the size of the list (the .Count property) and the value of the indexer. That may begin to give you an idea of what is going on.

You seem to be assuming that unitName and unitPrice have the same number of elements, but the exception suggests that this is not so. Figure out how unitName might have a Count greater than 0 even while unitPrice is empty, and you’ll probably find the source of your problem.

Both lists have the same size. They are public lists and I checked their size in the inspector during runtime. The odd thing is also that if my 2 lists have 1 property in it it won’t give me an error. The error happens when my lists have 2 or more values stored in it.

Also I’m not really sure were the error happens as unity wont jump to the error line when I double click on the error message in the console. The error desciption in the OP suggests that it should be in unitBuild method ?

You’re probably removing a unit in the middle of your loop and then accessing it later in the loop. You’ve given us some butchered version of the script, so we can’t tell for sure, but the error messages indicate that you call SpawnBullet() in your loop and then access the same element - that’s what’s causing the error.