Retrieving array element that was out of bounds

Hello everybody, when I ad the script “waypointcircuit” to my circuit (empty object that has 4 emptys) and I click the button “Assing using all child objects” I have the error “retrieving array element that was out of bounds” and this error message appear the number of times as children objects I have.

Another thing, how i do to make the car stop, it seems that the “target” field in the “waypointprogresstracker” script or in the “AI car control” script does not work or i am doing something wrong.

Any idea?
Thanks a lot!

1 Like

Don’t know what your “waypointcircuit” script is or does. Don’t know what “waypointprogresstracker” script is or does, and so on. Not sure I want to know what “Assing” is. :stuck_out_tongue:

But, the error means that you are attempting to access an array element using an index that is outside of the bounds of an array. For example, if your array has 4 elements they are indexed from 0 to 3. If you try to access an element at index 4 you will get this error. You’ll need to identify what part of your script is trying to do that. If you need help with that you will need to give us much, much more information.

3 Likes

Thank you Dave I know your answer but I do no know why the problem exists if that script supposedly is ok.

The watpointcircuit script is a script provided with Unity that let you make a circuit for example for a car race so the AI CAR can follow the circuit. You make an empty object (the circuit) and ad to it empty objetcs (The points to follow). You ad the script to the Circuit and by the button Assign… you establish the array (the points to follow).

How it can be wrong if the size of the array is determined by the points to follow (empty children objects).

“waypointcircuit” script and “waypointprogresstracker” script come inside Unity.

I hope i explained more clearly.

He means the WaypointCircuit class that comes with the Car AI portion of the Standard Assets package.

I’m unable to reproduce the error you’re reporting here. Here’s what I did:

  • Created a game object, added a WaypointCircuit component to that.
  • Created 4 empty child game objects
  • Clicked “Assign using all child objects”

Boom, they’re all added to the WaypointCircuit component. No errors.

Just about the only way I can create an error is by deleting a child object, but that’s easily fixable by hitting the assign button again.

What version Unity are you using?

1 Like

Wow thanks BlackPete. Look, i am using version 2017.1.0f3 but the one before this version (i don’t remember de number but was something 5.0.4?)gave me same error.

The only solution I gave on that line (of the waypointcircuit script) was adding (-1)…So the line would be “var item = items.GetArrayElementAtIndex(i-1);” The compiler did not give me any error of that kind.

I am having same problem. The error only appears if you click on the waypoint circuit in the editor hierarchy. I get the same error with unity version 5.4.3, 2017.1.1f1 and 2017.2.1f1. I’m interested in a solution to this problem because it may be the cause of another problem I have. If anyone has a fix for this or some insight I’m very very interested. Thanks!

for (int i = -1; i < items.arraySize; ++i)
{

var item = items.GetArrayElementAtIndex(i);//There is happened error.absorbing the"
Retrieving array element that was out of bounds"

I am replacing the “for (int i = -1; i < items.arraySize; ++i)” passing the “for (int i = 0; i < items.arraySize; ++i)”
So that! the fault fades away.
Welcome reply!

2 Likes

You may not need to worry about this error. It only appears if the waypoints list is selected in scene editor. It does not happen during gameplay. The -1 is required in the script so if you set the for loop to not include -1 part of your script may be broken. I think the reason it appears when waypoints is selected is it is designed to run in editor mode if I remember the script correctly.

i have the same problem, i also think “i” need -1, but “int” include -1, please give me a answer, 3q

You should try creating your own thread where you describe your issue and post any relevant code so people can try to help you. :slight_smile:

THANK YOUUUUUU

THANK YOUUUUUU

Just move the items.GetArrayElementAtIndex() line to the else block of i == -1. Who implemented the WaypointCircuit.cs of the vehicle standard asset? This is a fail. :smile:

Here’s the corrected snipped:

            if (i == -1) {
              EditorGUI.LabelField(rect, titles[n]);
            } else {
/* > */       var item = items.GetArrayElementAtIndex(i);
            if (n == 0) {

This is a quick solution to fix the issue. Be free to make it better.

3 Likes

This works for me, thankyou