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 OctoMan · May 16, 2017 at 10:08 PM · arraylistsizefor-loopargumentexception

How to prevent an overflow?

I have an array

 theArray = GetComponentsInChildren<Transform> ();

and create a list from it

 foreach(Transform path_obj in theArray)
         {
             if(path_obj != this.transform)
             {
                 path_objs.Add(path_obj);
 
             }
         }

When i want to fill another list, i need to overflow the array

    void Addpoints()
         {
             int linesteps = 10;
             Vector3 lineStart = path_objs[0].position;
 
         for (int j = 0; j < path_objs.Count; j++)
         {
             
             for (int i = 1; i <= linesteps; i++)
             {
                 Vector3 lineEnd = GetPoint(path_objs[j].position, path_objs[j + 1].position, path_objs[j + 2].position, i / (float)linesteps);
                 bezierObjs.Add(lineStart);
                 lineStart = lineEnd;
 
             }
             j++;
         }
     }

Since i need to go 1 and 2 higher than thearray/list path_objs is i get and argument out of range. All works fine, but i'd like to have a clean up.

Can i increase thearray by 2 anyhow or the list with empty fields? Or is there any other possible approach?

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 Ahndrakhul · May 17, 2017 at 12:12 AM 0
Share

I don't know what's going on in the GetPoint method, so I don't really want to post this as an answer. It looks like you could avoid getting index out of range errors by changing your for loops to something like this:

 for (int j = 2; j < path_objs.Count; j++)
         {
 
             for (int i = 1; i <= linesteps; i++)
             {
                 Vector3 lineEnd = GetPoint(path_objs[j - 2].position, path_objs[j - 1].position, path_objs[j].position, i / (float)linesteps);
                 bezierObjs.Add(lineStart);
                 lineStart = lineEnd;
 
             }
             j++;
         }

Are you mean to increment j twice?

avatar image OctoMan Ahndrakhul · May 17, 2017 at 07:15 AM 0
Share

First of thanks for your reply.

GetPoint takes 3 vectors as arguments to form 10 new lerped vectors inbetween the 3 given ones. Thats working and has actually nothing to do with the overflow i try to prevent.

"j" needs to be incremented twice yes.

Lets assume i have 3 transforms in thearray. From transform[0] to transform[2] i create 10 lerped vectors. Aftercompleted i increment j by 2 so the next startpoints is at transform[2] but there are no other points i can take and use, here comes the exception i want to prevent.

same when only 2 points there the 3rd is missing i needed to prevent it.

1 Reply

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

Answer by jdean300 · May 17, 2017 at 08:34 AM

path_objs.Add(null) to add an empty slot.

path_objs.Add(path_objs[path_objs.Count - 1]) to duplicate the last element in the list.

But what it seems like you want to do is stop j at an earlier index: for (int j = 0; j < path_objs.Count - 2; j++)

Comment
Add comment · Show 3 · 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 OctoMan · May 17, 2017 at 11:31 AM 0
Share

That's pretty close to what i need, actually i noticed i need both!

Adding NULL objects sadly gives nullreferences, adding last item, works kinda nice.

Just one last thing, is it possible to figure if the list has an even or uneven list size, and depending on this i add 1 or 2 placeholder items?

avatar image OctoMan OctoMan · May 17, 2017 at 11:38 AM 0
Share

I used modulo , yay all works now!

 if (path_objs.Count % 2 == 0)
         {
             //iseven
             path_objs.Add(path_objs[ path_objs.Count-1]);
         }
         else
         {
             //isuneven
             path_objs.Add(path_objs[ path_objs.Count-1]);
             path_objs.Add(path_objs[ path_objs.Count-1]);
         }


avatar image jdean300 OctoMan · May 17, 2017 at 06:22 PM 0
Share

Glad it's working!

If this has answered your question, please accept the answer so it shows as answered on the question listing.

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

114 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

Related Questions

Simple way to access data of different types within a nested array? 0 Answers

ArguentOutOfRangeException: Argument is out of range. Parameter name:index 1 Answer

Building tycoon game, need help storing variables into an array. 1 Answer

Why is my for loop not instantiating object at correct possision from List 1 Answer

Object list is null? 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