Move a GameObject for certain amount of time, and then move it again for a certain amount of time...

Hey, So, I tried to do this a horrible way (Looping... don't ask), and it didn't really work, but I was wondering what approach/how you would do this.

I need a GameObject to move left for like, 5 seconds, then right for 5 seconds... and back and forth...

If you have an idea on how to do this, please say so, I'll be thinking about it though...

Thanks, Justin W.

A quick way to go about this is to work with the Time. When you start moving store a float of the current time with Time.time.

float savedTime = Time.time;

From there you can do

if(Time.time - savedTime >= 5)

to know if 5 seconds to has passed since you set the variable. All of this should be put inside something like the update function.

function Update () {
float savedTime;
savedTime = Time.time;
if(Time.time - savedTime <= 5)
{
    transform.Translate(Vector3.forward * Time.deltaTime * 10);
 }
 else if (Time.time - savedTime >= 5 && Time.time - savedTime  <= 10)
 {
    transform.Translate(Vector3.forward * Time.deltaTime * -10);
 }
 else if (Time.time - savedTime > 10)
 {
    savedTime = Time.time;
 }
}

Gives error: Assets/Move.js(2,6): UCE0001: ';' expected. Insert a semicolon at the end.