I have the code below:
If i move the “PrepareToPlaceTheTemplate” code to a normal function (and remove the yield), not a coroutine the animation with multiple objects works but all objects are animated at the same time.
If i instead use the code as below and fire off the coroutine in the same place were the code was placed with “StartCoroutine (PrepareToPlaceTheTemplate ());” it only iterate the second coroutine once despite the fact that it should do it multiple times as it does when the same code, except the yield, is placed in a normal function.
Could someone please explain to me why?
IEnumerator PrepareToPlaceTheTemplate() {
int y = 0;
foreach (GameObject aGO in Singleton.Instance.master_GameObject_List) {
float XX = float.Parse (originalData_List [y + 1]);
float YY = float.Parse (originalData_List [y + 2]);
print ("HIT: " + aGO.tag + " | " + aGO.transform.position.x + " | " + XX);
if (!Mathf.Approximately(aGO.transform.position.x, XX) || !Mathf.Approximately(aGO.transform.position.y, YY)) {
GameObject move_GO = GameObject.Find(aGO.name + "_R");
Vector3 moveStartPosition_V3 = new Vector3(XX, YY, 0f);
Vector3 moveEndPosition_V3 = new Vector3(aGO.transform.position.x, aGO.transform.position.y, 0f);
StartCoroutine (AnimateTheObject(move_GO, moveStartPosition_V3, moveEndPosition_V3));
yield return new WaitForSeconds (0.2f);
}
y = y + 3;
}
}
float time = 0.2f;
IEnumerator AnimateTheObject(GameObject to_GO, Vector3 startPos_V3, Vector3 endPos_V3) {
float t = 0f;
while (t < 1) {
yield return null; //new WaitForSeconds (0f);
t += Time.deltaTime / time;
to_GO.transform.position = Vector3.Lerp (startPos_V3, endPos_V3, t);
}
}