Movement through an array over time

This is my current code, attached to a gameobject. It causes the gameobject to go from y position 10 to 22 but I want it to do that and then from 0 to 22, then from -20.5 to 22 and finally 13 to 22. Also, I want destination to instead point to the next array element(with the usual restriction to the array length). Thanks!

private var enemy001yMovement = new Array();
enemy001yMovement[0] = 10;
enemy001yMovement[1] = 0;
enemy001yMovement[2] = -20.5;
enemy001yMovement[3] = 13;
var destination = 22;

function Update()
{
for(var pointer in enemy001yMovement)
{
transform.position = Vector3(0,Mathf.Lerp(pointer,destination,Time.time),0);
}
}

Like Ben said, you want to track the index into the array and as update is called each frame, it will take care of acting over time.

Something like this:

Unity js

private var yWaypoints = new Array();
yWaypoints[0] = 10;
yWaypoints[1] = 0;
yWaypoints[2] = -20.5;
yWaypoints[3] = 13;
var index = 1; //Index into the array at the destination

//Called each frame
function Update()
{
    //If we've gone through the array, we're done
    if(index < yWaypoints.length)
    {
        //Move from the previous position in the array to the next over time
        transform.position.y = Mathf.Lerp(yWaypoints[index-1],yWaypoints[index],Time.time);

        //If we've reached the destination, move to the next one
        if(transform.position.y == yWaypoints[index]) index++;
    }
}

It's worth taking the time to understand skovacs1's answer. If you don't understand when Update is called and how to use it, you won't get too far with Unity scripting.

For the specific problem of moving objects from place to place over time, which you will encounter over and over again, you might also want to look at iTween, which is free. The command to move a game object to position 0,22,0 over the next 2 seconds would look something like this: iTween.MoveTo(gameObject,Vector3(0,22,0),2);

You can queue up multiple movements, have the object start fast then slow down as it reaches the destination, and other effects. (Disclaimer: I'm planning to use it on my next project, and I expect it will save me a lot of time, but haven't have much hands-on with it yet.)