Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jenkins6161 · Sep 19, 2013 at 06:30 PM · javascripterrorclassgeneric list

Argument out of range - Generic list (JS)

I'm using a class to organize many arrays into an object. Then using that object to tell my game what to do at certain stages. This error is thrown ArgumentOutOfRangeException: Argument is out of range and I have no idea why. Here's my code for accessing the list:

 import System.Collections.Generic;
 
 var skin: GUISkin;
 
 function OnGUI() {
     GUI.skin=skin;
     var item = Stage.Stages[0];
 }

And this is my code for the stage class:

 import System.Collections.Generic;
 
 class Stage
 {
     var controlArray: String[];
     var taskName: String[];
     var taskStatus: Texture2D[];
     var PC: boolean;
     var arrowArray: GameObject[];
 }

 //what a stage object might look like
 var Stage16 = new Stage();
 Stage16.controlArray[0] = "";
 Stage16.taskName[0] = "findTheWaterCooler";
 Stage16.taskName[1] = "returnToYourDesk";
 Stage16.taskName[2] = "dropToTheGround";
 Stage16.taskName[3] = "coverUnderTheDesk";
 Stage16.taskName[4] = "holdDuringTheEarthquake";
 Stage16.taskName[5] = "dustYourselfOff";
 Stage16.taskStatus[0] = Resources.Load("check_box_checked");
 Stage16.taskStatus[1] = Resources.Load("check_box_failed");
 Stage16.taskStatus[2] = Resources.Load("check_box_checked");
 Stage16.taskStatus[3] = Resources.Load("check_box_checked");
 Stage16.taskStatus[4] = Resources.Load("check_box_checked");
 Stage16.taskStatus[5] = Resources.Load("check_box_empty");
 Stage16.PC = false;
 Stage16.arrowArray[0] = null;


 static var Stages : List.<Stage> = new List.<Stage>(21);
 Stages[0] = Stage0;
 Stages[1] = Stage1;
 Stages[2] = Stage2;
 .
 .
 //and so on

I don't understand why its giving me a index out of range error?? Please help!

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 perchik · Sep 19, 2013 at 07:28 PM 0
Share

what is the full text of the error? What argument is it saying is out of range?

avatar image Jenkins6161 · Sep 19, 2013 at 08:19 PM 0
Share

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Stage].get_Item (Int32 index) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/List.cs:633) tasks.OnGUI () (at Assets/Scripts/tasks.js:32)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by perchik · Sep 19, 2013 at 07:32 PM

I'm not as familiar with Javascript, but it seems that you'd have to initialize the array before accessing it:

Ie, doing this,

 var taskName: String[]

and then this:

 Stage16.taskName[0] = "findTheWaterCooler";
 Stage16.taskName[1] = "returnToYourDesk";
 Stage16.taskName[2] = "dropToTheGround";
 Stage16.taskName[3] = "coverUnderTheDesk";
 Stage16.taskName[4] = "holdDuringTheEarthquake";
 Stage16.taskName[5] = "dustYourselfOff";

seems like a problem. I think you have to initialize it first:

 taskName = new String[6]
Comment
Add comment · Show 1 · 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 Jenkins6161 · Sep 19, 2013 at 08:23 PM 0
Share

did this

 var Stage16 = new Stage();
 var controlArray: String[];
 var taskName: String[];
 var taskStatus: Texture2D[];
 var PC: boolean;
 var arrowArray: GameObject[];
 Stage16.controlArray[0] = "";
 Stage16.taskName[0] = "findTheWaterCooler";
 Stage16.taskName[1] = "returnToYourDesk";
 Stage16.taskName[2] = "dropToTheGround";
 Stage16.taskName[3] = "coverUnderTheDesk";
 Stage16.taskName[4] = "holdDuringTheEarthquake";
 Stage16.taskName[5] = "dustYourselfOff";
 Stage16.taskStatus[0] = Resources.Load("check_box_checked");
 Stage16.taskStatus[1] = Resources.Load("check_box_failed");
 Stage16.taskStatus[2] = Resources.Load("check_box_checked");
 Stage16.taskStatus[3] = Resources.Load("check_box_checked");
 Stage16.taskStatus[4] = Resources.Load("check_box_checked");
 Stage16.taskStatus[5] = Resources.Load("check_box_empty");
 Stage16.PC = false;
 Stage16.arrowArray[0] = null;

and got this error: Assets/Scripts/Stage.js(172,5): BCE0089: Type 'Stage' already has a definition for 'controlArray'. for each new var

avatar image
0

Answer by DaveA · Sep 19, 2013 at 07:34 PM

Try breaking this

static var Stages : List. = new List.(21);

up into this:

 static var Stages : List.<Stage>;
 
 function Start()
 {
  Stages = new List.<Stage>(21);
 }
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 Jenkins6161 · Sep 19, 2013 at 08:16 PM 0
Share

then i get a NullReferenceException: Object reference not set to an instance of an object

avatar image DaveA · Sep 19, 2013 at 08:24 PM 0
Share

What perchik said too. But don't use 'new' outside the scope of a function in general.

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

17 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

Related Questions

ArgumentOutOfRangeException - Generic List - Javascript 1 Answer

Public Class Error 1 Answer

Access function (and variable) outside class in the class in Javascript. 1 Answer

BCE0005: Unknown identifier: 'spriteRenderer'. 2 Answers

Unity freezes when I add a special script 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