Cryptic index error, SOS!

Note: This is a re-post that I submitted to the Iphone forum that is still awaiting a helpful response.

After a long time fiddling with it, last night, for the first time since I started with unity script about a month ago, I was able to draw a line on the screen using the Linerender that followed a touch drag and stays on screen until another touch starts.

The script is very close to being ready to give out to the community as my first contribution and I think also as needed script for the noob public in general. But it has a couple of errors that I’m stumped by.

I’ve taken it about as far as I can. The script works but I’m getting errors that I don’t understand, even after a bunch of searches through this forum and the excellent Custom Google Unity Search at Programmable Search Engine

The first is:

ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
9

Whatever the index name this always seems to reference these lines in script:

Code:
drawLine.SetPosition(positionToFill, rawTouchArray[arrayNumber]);
         drawLine.SetPosition(positionToFill +1, rawTouchArray[arrayNumber +1]);

Is there something wrong with using a created var to define the linerenderer vertex position?

The other error is also related to the same script lines:

LineRenderer.SetPosition index out of bounds!
UnityEngine.LineRenderer:SetPosition(Int32, Vector3) (at /Users/build/builds/unity-iphone/iphone/Runtime/Export/Generated/Graphics.cs:1901)
BBTouchCapture:BuildLine() (at Assets/iPhone Standard Assets/Scripts/BBTouchCapture.js:71)
BBTouchCapture:Update() (at Assets/iPhone Standard Assets/Scripts/BBTouchCapture.js:20)

If someone could please help resolve this error I’d very much appreciate it. I can’t post it in good conscience as a cut and paste usable script or move on with the rest of my project until I clear this bugger!

Here’s the entirety of the script:
Code:

static var rawTouchArray = new Array ();
private var clearArray : boolean;
private var drawLine : LineRenderer;
private var pointsOnLine : int; //capture the total touches number to set the segment total
private var aCell : Vector3;


function Update ()
{         
   for (var touch : iPhoneTouch in iPhoneInput.touches)
   {   
      if (iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Began || touch.phase == iPhoneTouchPhase.Moved)
      {
         ClearArray();
         var multiTouchEnabled = false; // this still doesn't work!  FARTNUGGETS!!!
         aCell = (new Vector3 (touch.position.x-240, 5, touch.position.y-160));
         var fingers = iPhoneInput.touchCount;
         rawTouchArray.Push (aCell); //push the position from aCell into the array, appending onto the end
         BuildLine();
      }
      else if (iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Ended || touch.phase == iPhoneTouchPhase.Canceled)
      {
         clearArray = true;
      }
      // Debug.Log ("Array Total " + rawTouchArray.length + " Adjusted points total " + pointsOnLine);
      }      
}
private var destroyLine : boolean;//NOT HOOKED UP

function ClearArray() //cleans out the rawTouch array so a new swipe can be recorded
{
   if (clearArray)
   {
      rawTouchArray.Clear();
      clearArray = false;
      destroyLine = true;
      //Destroy (drawLine);  //may not need this yet
   }
}

private var positionToFill : int;
private var arrayNumber : int;
var windWidth = 10;

function BuildLine()
{
   drawLine = gameObject.GetComponent("LineRenderer");
   pointsOnLine = rawTouchArray.length;
   drawLine.useWorldSpace = false;
   drawLine.SetWidth (windWidth, windWidth);
   arrayNumber = 0;
   positionToFill = 0;
   
   if (pointsOnLine%2 == 0) //checks if pointsOnLine is even and if not makes it so by subtracting 1.
   {
   }
   else
   {
      pointsOnLine --;
   }
   
   drawLine.SetVertexCount(pointsOnLine);
   
   if (pointsOnLine >= 2)
   {   
      for (positionToFill = 0; positionToFill <= pointsOnLine;)
      {
         drawLine.SetPosition(positionToFill, rawTouchArray[arrayNumber]);
         drawLine.SetPosition(positionToFill +1, rawTouchArray[arrayNumber +1]);
         positionToFill ++;
         arrayNumber ++;
         Debug.Log ("pointsOnLine " + pointsOnLine + " positionToFill " + positionToFill);
      }   
   }   
}

You’re trying to read a position in the array which simply doesn’t exist.

Try printing the length of the array and arrayNumber+1 to the console before you try to access the array. You’ll most likely find that arrayNumber+1 is greater than or equal to the length of the array.

Remember, indexes start at 0, so the last position you can call is array.length - 1

Hi tonyd, thanks for your help. I’m going to go through your reply line by line so I don’t miss any! :smile:

I’m pretty sure that the array does exist, if it didn’t the for wouldn’t loop since it’s incrementing against a var that is set by the array.length.

I don’t understand. Does printing it beforehand make it more accessible somehow? (Actually, I tried it, the answer is no.)

I would assume that the array +1 would be at array length+1? I tried it but odd sidenote, when I do a +1 it seems to increment the variable by 10 instead of one. Please advise as to how to proceed.

Yeah, I found that out the hard debugging way when I was tracking down this (index out of bounds!) a week ago.

Thanks for the tip! -1 means infinity, right? Should I set something up so that it doesn’t call the last position? Or am I calling the last position too soon? I’ll bang away on this for a bit.

Thanks again tonyd, you are a lifesaver! Once I get this working I feel like I’m right on the verge of getting something real going twords my project.

tldr, but I will try to answer anyway :slight_smile:
what tonyd was saying is that the array position (index) doesn’t exist, not that the array doesn’t exist. For example in an array of 2 elements A it doesn’t make sence to write A[2]

Looks like you are setting a value for the positions array of the line renderer that it doesn’t like. The positions array has only 2 elements by default. You can increase the size of the array in the inspector.
As, tonyd suggested, you should print the positionToFill variable and see why it goes outside the bounds of the positions array of the line renderer.

No, -1 does not mean infinity.

array.length - 1 means subtract one from the length of the array to get the last index number you can use without producing an error.

var myArray = Array("first position", "second position", "third position");

myArray[0] is “first position”
myArray[1] is “second position”
myArray[2] is “third position”

But the length of the array (myArray.length) is 3.

Index 3 is actually the fourth position in the array, and if you try to call it, you will get an error. Which is why you must subtract 1 from the length to get the last index of the array.

Ah, thanks for the clarification. My used to programmers giving me tuning widget in code where -1 meaning infinity. I’m right in the middle of your scripting for noob btw. Thanks for that as well!

Yup yup yup! In my last attempt to fix it I thought I wasn’t going over anymore on the array but I still was since my new variable was still incremented by 1. Waht fixed it was I changed the script from:

for (positionToFill = 0; nextPositionToFill <= pointsOnLine;)

to:

for (positionToFill = 0; nextPositionToFill < pointsOnLine;)

and it stopped trying to access the last entry in the array. Thanks to everyone for the help. :smile: I’ve now added and entry to my personal scripting cheat sheet that says “never ever try to access the last entry in any Array!”