Line following object

Hey guys,

I tried to make a script which draws a line behind an object tracking its way. This is the error:
“InvalidCastException: Cannot cast from source type to destination type.”
Now I found another thread but it was another function so it didn’t really help. Im very new to the unity scripting so i guess its a false type of variable or something but im not sure.
Here’s the code:

var posArr = new Array();
var oldPosition;
function Start () {
    oldPosition = GameObject.Find("satellite").transform.position;
    Debug.Log("Started LINE");
}

function Update () {
    posArr.Push(GameObject.Find("satellite").transform.position);
    for (var value in posArr) {
        Debug.DrawLine(posArr[value],oldPosition,Color.red);
        oldPosition = value;
    }
}

On which line are you getting the error?

EDIT: I’m don’t do UnityScript but it seams you’re confusing for with foreach. Looks like you’re trying to use foreach but you used for keyword instead and then you should just use:

Debug.DrawLine(value,oldPosition,Color.red);
//instead of:
//Debug.DrawLine(posArr[value],oldPosition,Color.red);

Or not couse I don’t do UnityScript.

1 Like

At line 11

Yeah that seams to confirm what I said. Try using foreach keywaord instead of for (this is probably not related to problem) and inside the loop your ‘value’ variable already gets assigned subsequent values of array you are interating through - that’s the idea behind foreach so no need to reference the array just the ‘value’ variable

Thank you. There are no more errors, but neither is there a line.

Script looks fine. Perhpas it is rendering but you’re looking at wrong window? The debug class is generally for rendering in Editor so you should see the lines in Scene window but not in Game window.