- Home /
Argument Out of Range when accessing List
In Script One I create a "Big List" of 15 items, each item contains three Integers -- at present, it's merely meaningless test data.
In Script Two, I print the entire list, 15 items, three integers each -- no problem, the list picks up the data and prints it perfectly...random numbers or assigned numbers, I have no problem at all extracting the full list and printing it accurately.
In Script Three I create a "Small List" of three items selected from the "Big List" in Script One and I get the message "ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"
Here is script one:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScriptOne : MonoBehaviour
{
public List<NumTestClass> bigList = new List<NumTestClass> ();
void Start()
{
for (int i = 0; i < 15; i++)
{
int num01 = 11;
int num02 = 22;
int num03 = 33;
bigList.Add (new NumTestClass (num01, num02, num03));
}
}
}
public class NumTestClass
{
public int numberOne {get; set;}
public int numberTwo {get; set;}
public int numberThree {get; set;}
public NumTestClass (int one, int two, int three)
{
numberOne = one;
numberTwo = two;
numberThree = three;
}
}
Here is script two: ///////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
public class ScriptTwo : MonoBehaviour
{
void OnGUI()
{
for (int i = 0; i < 15; i++)
{
string tempString01 = GetComponent<ScriptOne> ().bigList [i].numberOne.ToString("F1");;
string tempString02 = GetComponent<ScriptOne> ().bigList [i].numberTwo.ToString("F1");
string tempString03 = GetComponent<ScriptOne> ().bigList [i].numberThree.ToString("F1");
GUI.Label (new Rect (25, i * 25 + 100, 150, 20), tempString01 + " - " + tempString02 + " - " + tempString03);
}
}
}
Here is script three //////////////////////////////////////////////////////////////////////
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScriptThree : ScriptOne
{
public List<RaceTestClass> smallList = new List<RaceTestClass> ();
void Start ()
{
int counter = 0;
while (counter < 3)
{
int temp01 = GetComponent<ScriptOne>().bigList[counter].numberOne;
int temp02 = GetComponent<ScriptOne>().bigList[counter].numberTwo;
int temp03 = GetComponent<ScriptOne>().bigList[counter].numberThree;
smallList.Add(new RaceTestClass (temp01, temp02, temp03));
counter++;
}
}
}
public class RaceTestClass
{
public int thingOne { get; set;}
public int thingTwo {get; set;}
public int thingThree {get; set;}
public RaceTestClass (int one, int two, int three)
{
thingOne = one;
thingTwo = two;
thingThree = three;
}
}
This is because the Start()
function in ScriptThree
is being called before the Start()
function in ScriptOne
.
To fix the problem you can move everything from Start()
in ScriptOne
to Awake
.
Answer by UnityCoach · Jan 03, 2017 at 10:28 PM
This looks a lot like a Script Execution Order issue.
Script3 is accessing members of Script1 upon Start (), while Script1 may execute after Script3.
Best option is to initialise your data in Script1 in Awake(). All Awake calls are called before any Start call.
Otherwise you can also go into Script Execution Order, and force the order so that Script3 gets called after Script1.
Thanks for your prompt response. I'll look at this right away.
Unbelievable. It works perfectly after changing one item in script one...I changed "Start" to "Awake" and it works wonderfully.
I've been working on this since last night. I don't even know how to begin to thank you.
Thanks again. I've been throwing everything I have at this (which isn't much, probably). I've tried changing inheritance, using Arrays, all sorts of stuff...I've been looking up stuff all day. Nothing worked.
I didn't even realize the start/awake functions could impact the different scripts that way.
A great lesson has been learned (by me). :)
You're welcome. We've all been there :) Game Engines work a bit differently from Application Development Standards.
There's always that first frame "Start", and before this the Awake. Same applies to Update and LateUpdate.
Answer by LK84 · Jan 03, 2017 at 10:30 PM
The Start() methods of both scripts are executed in an arbitrary order. Hence if the Start() method of scrpt 3 gets called first you are trying to access the List from Script 1, which is empty at this point. Hence you get the Argument Out of Range Error. To solve that you can either set the Script excution order manually:https://docs.unity3d.com/Manual/class-ScriptExecution.html or (what I would prefer) implement are logic where the Lists get filled in te right order.
Wow. Thanks for your rapid response. I'll look at your suggestion immediately.
Thank you. You both answered my question accurately. I wish I could "Accept" both but I received the other answer one $$anonymous$$ute earlier.
Regardless, I've been working on this for many hours and I appreciate your kind, rapid and accurate assistance.
Best wishes.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
object assigns but does not show when added to list, or any other var 0 Answers
using classes in List<>'s 1 Answer