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
2
Question by Sintua · Oct 14, 2011 at 09:11 PM · listarrays

(C#) ArgumentOutOfRangeException: Parameter name: Index

So this problem has been driving me crazy. I have some problem with trying to use a simple list, so I distilled it down to the most basic script and it's STILL giving me problems. I've looked at other examples but the problem is either something else or the code is too complicated to actually see what's going on.

Basically I'm making a list, putting numbers in it (from 0 to 5), and printing those numbers to the console.

The result of running the below script is:

Console: 5 (printing "iterations")
Console: 0(printing the first value of "i")
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

Script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class arrayTests : MonoBehaviour {
     
     //List<int> testNumberList;//make the list<int>
     //List<int> testNumberList = new List<int>();//make the list<int>
     //same problem if done outside or inside "start", or declared outside and initialized inside.
 
 // Use this for initialization
 void Start () {
     
     List<int> testNumberList = new List<int>();
     
     int iterations = 5; //go through 5 iterations
     print(iterations.ToString()); //print this to console
     
 
     for (int i =0; i<iterations; i++)//for however many iterations (5)
     {
         print(i.ToString());//what "i" are we on?
         testNumberList[i] = i;//put "i" in the list
         print(testNumberList[i]);//prove that "i" is in the list now
     }
 }
 
 // Update is called once per frame
 void Update () {
 
     }
 }
Comment
Add comment · Show 1
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 Sintua · Oct 14, 2011 at 09:33 PM 0
Share

I tested explicitly declaring the capacity of the list, even though the whole point of using them is you dont have to, and it still wont work.

tried: List testNumberList = new List(10); yet it still says that trying to put infomration in index at 0 is "out of range"?

1 Reply

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

Answer by Graham-Dunnett · Oct 14, 2011 at 09:42 PM

Can you set a value in the List using Array notation? I'd use:

        testNumberList.Add( i);//put "i" in the list

When you try to access the i'th element to set it using array notation, surely it does not exist (yet)? If you want to set at a given index position use:

        testNumberList.Insert(i,i);
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 Sintua · Oct 14, 2011 at 09:49 PM 1
Share

You sir are a gentleman and a scholar. I changed the for loop to:

for (int i =0; i

and it works perfectly!

I was under the (mistaken) impression that i was simply adding the value to the list, but you're right, now I see what I was actually trying to do was ASSIGN "i" INTO the current [0] slot, when the list in fact had no slots. Thank you, you won't believe how much this little problem has been driving me crazy!

avatar image LangNoob · Aug 03, 2016 at 07:45 PM 0
Share

Thank you! This really helped me out.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Need help with converting an array to a list in C#` 4 Answers

sort list by variable in list of ints array 2 Answers

Need some help looping through an array or list 2 Answers

Dynamic update List<> 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