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?
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?
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.
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++)
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?
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]);
}
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
Follow this Question
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