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 /
avatar image
0
Question by Topthink · Jan 03, 2017 at 10:19 PM · c#listclass

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;
     }
 }









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 TBruce · Jan 03, 2017 at 10:49 PM 0
Share

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.

avatar image Topthink TBruce · Jan 03, 2017 at 11:08 PM 0
Share

Thank you kind sir. I appreciate your help.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
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 Topthink · Jan 03, 2017 at 10:45 PM 0
Share

Thanks for your prompt response. I'll look at this right away.

avatar image Topthink · Jan 03, 2017 at 10:55 PM 0
Share

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.

avatar image Topthink · Jan 03, 2017 at 11:11 PM 0
Share

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). :)

avatar image UnityCoach Topthink · Jan 03, 2017 at 11:26 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 2 · Share
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 Topthink · Jan 03, 2017 at 10:44 PM 0
Share

Wow. Thanks for your rapid response. I'll look at your suggestion immediately.

avatar image Topthink · Jan 03, 2017 at 10:57 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

267 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I make a list of Classes or Scripts? 3 Answers


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