title: How to transform a gameObject to a desired position and return to starting position?
body: <p>I'm new to UnityScript and I have the following code that does a forward movement of a gameObject on "Z" axis, but needs some refinement. Let me explain.</p>
  
  <p>The script runs when a GUI.Button is clicked. The gameObject starts moving...till infinity. 
  I have tried to make it move till a desired pos, (e.g. an empty gameObject pos) but didn't work at all.</p>
  
  <p>How should I refine this code snippet to move the gameObject to a desired position on a first GUI.Button click and return to starting position on back click? </p>
  
  <p>Furthermore, is it possible to have this movement made step by step on same GUI.Button clicks?</p>
  
  <p>Here is the code snippet:</p>
  
  <pre><code>#pragma strict
  
  // member variables (declared outside any function)
  var startPos: Vector3;
  var endPos : Vector3;
  var cachedTransform : Transform;
  private static var isforward = false;
  
  // save pos before moving:
  startPos = transform.position;
  
  // make the gameObject transform, then restore the initial position when needed:
  transform.position = startPos;
  
  function Awake() {
  startPos = transform.localPosition;
  }
  
  function Start() {
  cachedTransform = transform;
  startPos = cachedTransform.position;
  }
  
  function FixedUpdate() {
  
  if(isforward){
  
  var translation : float;
  
  if (cachedTransform.position.x == endPos)
   {
    cachedTransform.position = startPos;
   }
   else
   {
      translation = Time.deltaTime * 2;
      cachedTransform.Translate(0, 0, translation);
      cachedTransform.Translate(Vector3.forward * translation);
   }
  }
  }
   static function doforward ()
  {
      isforward = !isforward;
  }
  </code></pre>
  
  <p>Thank you all in advance for your answers.</p>