Quantcast
Channel: Questions in topic: "script referencing"
Viewing all articles
Browse latest Browse all 41

Problem when acessing a list from another script? (ArgumentOutOfRangeException)

$
0
0
------ **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 randOrder; void Start() { randOrder = new List(); 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(); //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)

Viewing all articles
Browse latest Browse all 41

Trending Articles