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);
}
}
}