The question is answered, right answer was accepted
Problem when acessing a list from another script? (ArgumentOutOfRangeException)
Edit: With the tip given by @gjf, i have changed in the parent script the function "Start" to "Awake", in this way i guarantee that the list is created before the child objects try to load, preventing the error posted below.
Ok guys, my situation is this:
I have an single empty GameObject that hold another three empty GameObjects (childs).
When i'm acessing a list inside the parent object trough the script inside the childs, or give a debug.log for example, it returns this error:
"ArgumentOutOfRangeException: Argument is out of range. Parameter name: index"
The parent has this script attached to him:
public class Master : MonoBehaviour {
public List<int> randOrder;
void Start() {
randOrder = new List<int>();
for(int i=0;i<3;i++){
randOrder.Add(Random.Range(1,4));
}
}
}
The other three child objects has this script attached to then:
public class childObject : MonoBehaviour {
public Master masterRef; // hold the reference to the Master script.
void Start(){
masterRef = gameObject.GetComponentInParent<Master>(); //Reference of the Master script.
Debug.Log(masterRef.randOrder[0]); // The error happens here
Debug.Log(masterRef.randOrder[1]); // And here
Debug.Log(masterRef.randOrder[2]); // And here
}
}
What could be the problem? The list seems to be empty when accessed by another script, although it is not.
The complete error is this:
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-runtime-and-classlibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
you shouldn't assume that the script creating the List
runs before the child objects accessing it...
So you are saying that i'm supposed to change the script execution order in the project settings or maybe change the master script to execute inside an Awake?
Follow this Question
Related Questions
List update 0 Answers
Error in Inventory Script 1 Answer
What is the most effective way to structure Card Effects in a Single Player game? 1 Answer
list.contains problem 1 Answer
Unknown Argument Out of Range Index Error On Card Game 1 Answer