Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Nov 18, 2015 at 06:19 PM by Douglas-William for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Douglas-William · Nov 18, 2015 at 06:29 AM · c#unity 5listargumentoutofrangeexceptionscript referencing

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)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Nov 17, 2015 at 06:17 PM 1
Share

you shouldn't assume that the script creating the List runs before the child objects accessing it...

avatar image Douglas-William gjf · Nov 17, 2015 at 07:17 PM 0
Share

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?

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges