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 /
avatar image
0
Question by Marcwasmasjien · Aug 01, 2016 at 02:47 PM · arrayarray-out-of-range-exceptarray list

Array index out of range

 StreamReader fileReader = new StreamReader(filepath, true);
 
         //read the text file
         filecontent = fileReader.ReadToEnd();
 
         //close the file when done reading
         fileReader.Close();
         
 
         //split the text file into lines
         lines = filecontent.Split("\n"[0]);
 
 
         //list of nodes and int's
         List<Vector3> nodesList = new List<Vector3>();
         List<int> intList = new List<int>();
         int mode = 0;
 
         Debug.Log(values.Length);
 
         for (int l = 0; l < lines.Length;l++)
         {
             
             values = lines[l].Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
             if( values[0] == "NODES")
             {
                 mode = 1;
                 break;
             }
             
             if( values[0] == "TRIANGLES")
             {
                 mode = 2;
                 break;
             }


i get the error(Array index out of range) at the if(values[0] == Nodes)

i don't understand why it is giving the error.

I would be very thankfull if somebody could explain what i did wrong.

thank you in advance

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 Glurth · Aug 01, 2016 at 03:49 PM 0
Share

Regarding the error itself: The error indicates that the value array, exists, but it is empty.
If it were NOT empty, and had at least one items on it, then an index value of [0], would NOT be out of range.

e.g existant, but empty array

 List<int> intList = new List<int>();
 intList[0] = 42;   //generates an out of range error

e.g. existant, NOT empty array

 List<int> intList = new List<int>();
 intList.Add(42);
 intList[0] = 42;  // we have added an element to the list, so this index is now valid.
 intList[1] = 42;   //generates an out of range error
avatar image jprocha101 · Aug 01, 2016 at 04:07 PM 0
Share

The debugger is your best friend. $$anonymous$$ore than likely lines[1] is not being split up as expected. Debug this and you will see what lines[1] is and why it does not set values.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by adriono · Aug 01, 2016 at 07:09 PM

Maybe Split return empty array and you try to access to first element which just isn't exist. You should check it earlier:if(values.Length > 0). I hope this helps.

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Accessing the array of a null item / Moving items in array 0 Answers

Fill Array Of Arrays With Method/Function 0 Answers

how to RemoveAt 2 Answers

Array index is out of range? 4 Answers

Array index is out of range C# 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