# Howcome this coroutine loops once?

I’m having a bit of a hard time understand coroutine and lerp. This IEnumerator should iterate several times, but the loop only ever fires once. Also not sure if the Lerp is right.

``````IEnumerator move_item()
{

for (float t = 0; t < 1; t += Time.deltaTime / 1f)
{

recievedItem.transform.position = Vector3.Lerp (recievedItem.transform.position, transform.position, t);
yield return null;
StartCoroutine (move_item());
}

yield return new WaitForSeconds(1f);
}
``````

Ah now I see. You’re using Lerp incorrectly. While using it, both the StartPosition and EndPosition should stay the same, only t should change. See http://wiki.unity3d.com/index.php?title=MoveObject and can someone explain how using Time.deltaTime as 't' in a lerp actually works? - Unity Answers as examples.

In your case I’d use Vector3.MoveTowards, unless you absolutely needs to use Lerp.
Something like this

``````IEnumerator move_item()
{
float speed = 5f;
float step = Time.deltaTime * speed;
while(Vector3.Distance(recievedItem.transform.position, transform.position) > 0.01f)
{
recievedItem.transform.position = Vector3.MoveTowards(recievedItem.transform.position, transform.position, step);
yield return null;
}
}
``````

Generally, you would want the Lerp method to have a stop condition related to the distance from object to target

``````IEnumerator move_item()
{
if(Vector3.Distance(recievedItem.transform.position, transform.position) > 0.01f)
{
recievedItem.transform.position = Vector3.Lerp(recievedItem.transform.position, transform.position, Time.deltaTime);
yield return null;
StartCoroutine(move_item());
}
}
``````