# Dynamically updating a Coroutine's parameters every frame

Hello!

How can we update the values of pointA & pointB every frame to feed into our coroutine?

These two coroutines are called on IEnumerator Start() which animate a square in an endless loop between two objects. Sometimes objectA and objectB move, and we need the square animation to update it’s start and end coordinates every frame.

Right now, we get new coordinates fed at the beginning of the While loop, which gives us a messy transition. Thus, looking for a way to change them more frequently!

Thanks for the help in advance.

``````public GameObject objectA;
public GameObject objectB;

IEnumerator Start()
{
while (true)
{
pointA = objectA.transform.position;
pointB = objectB.transform.position;

yield return StartCoroutine(startMove(transform, pointA, pointB, timeToPointB));
yield return StartCoroutine(endMove(transform, pointB, pointA, timeToPointA));
}
}

IEnumerator startMove(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i= 0.0f;
var rate= 1.0f/time;

yield return new WaitForSeconds (startPosPause);

while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
yield return new WaitForSeconds (endPosPause);
}

IEnumerator endMove(Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
var i= 0.0f;
var rate= 1.0f/time;

yield return new WaitForSeconds (startPosPause);

while (i < 1.0f)
{
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield return null;
}
yield return new WaitForSeconds (endPosPause);
}
``````

You can’t change the arguments that were passed into the coroutine, but you could add some fields to your class and have the coroutine check those.

Example using args:

``````IEnumerator DoStuff(int x) {
while (x != 0) {
//do something

x--;
yield return null;
}
}
``````

Example using fields:

``````int x;

IEnumerator DoStuff() {
while (x != 0) {
//do something

yield return null;
}
}
``````

You have to be careful, though, as that second example wouldn’t self-terminate. It would only end if some other piece of code told it to (by setting `x` to zero).

There are many ways you could organize this. The key idea is that you can allow other parts of your code to change those values, as long as they’re in scope for both the coroutine and the other code. It’s a bit risky if you’re not careful.