Wrong with my script to move between A and B point

Dear all
I’d like to make a function that my enemies walk back and forth in a 2d game.
My idea is that the enemy walk from start,and after it moves to target,it turns back and walks to start,and so on.
Here is my script:

function Update () {
var start:Transform;
var target = GameObject.FindWithTag ("target").transform; 

var dist : float = Vector3.Distance(start.position, target.position);
...var go :float=Vector3.Distance(target.position, transform.position);
var offset :float;
offset=dist-go;
if(offset>0){
transform.position.x+=0.1;}
if(dist-offset<0){
transform.position.x+=-0.1;}

}

Could anyone help me with this function?Thx!

A better way is to define the object position with Vector3.Lerp(start, end, pos): this function calculates the position between start and end according to the value of pos - 0 is start, 1 is end, 0.5 is mid way, etc. Make pos vary continuosly between 0 and 1, and you’ll get your enemy “pingponging” between the two positions:

var duration: float = 5; // travel time in seconds
private var target: Vector3; // end position
private var start: Vector3; // start position
private var pos: float = 0; // relative pos (0=start, 1=end)
private var dir: float = 1; // direction (1:start->end, -1:end->start)

function Start(){
  start = transform.position;
  target = GameObject.FindWithTag ("target").transform.position;
}

function Update(){
  pos += dir*Time.deltaTime/duration; 
  if (pos<0 || pos>1) dir = -dir; // if reached start or end revert direction
  transform.position = Vector3.Lerp(start, target, pos);
}