So I have this coroutine that moves an object to a place, and I do it for a list of objects, but I want it to move them one by one (aka wait until previous coroutine is done before starting a new one) but adding any yields just stops the whole thing... im a bit lost to why.
Ive tried adding "yield return new WaitUnitl()" or "WaitForSeconds" but wherever i try to place it it either makes it wait before moving everything at once or they just stop moving all at once
Moving code:
public IEnumerator MoveObject(Vector3 source, Vector3 target, float overTime)
{
float startTime = Time.time;
while (Time.time < startTime + overTime)
{
transform.position = Vector3.Lerp(source, target, (Time.time - startTime) / overTime);
yield return null;
}
transform.position = target;
}
called in this for loop:
for (int i = 0; i < CardsInHand.Count; i++)
{
Card c = CardsInHand[i];
Vector3 target = new Vector3(startt + (1.5f * i), transform.position.y);
StartCoroutine(c.MoveObject(c.transform.position, target, 1));
c.GetComponent<SpriteRenderer>().sortingOrder = i;
}
Expect them to move one at a time, not all at once
Edit: well I had the biggest fart ever.... i forgot to use StartCoroutine() after making the method a coroutine... and i kept wondering why it wont move
To await a Coroutine you want to change the method you're currently in to be in a Coroutine, and then yield the new coroutine like this:
IEnumerator MyMethod()
{
for (int i = 0; i < CardsInHand.Count; i++)
{
Card c = CardsInHand[i];
Vector3 target = new Vector3(startt + (1.5f * i), transform.position.y);
yield return StartCoroutine(c.MoveObject(c.transform.position, target, 1));
c.GetComponent<SpriteRenderer>().sortingOrder = i;
}
}
From this answer by @Everts:
When creating a coroutine, Unity attaches it to a MonoBehaviour object. It will run first on call fo the StartCoroutine until a yield is hit. Then it will return from the coroutine and place it onto a stack based on the yield.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.