1

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

Sarm
  • 13
  • 3

1 Answers1

1

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.

Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
  • 3
    Seems like a good solution, but i don't see how lerping a position and then waiting for the next frame with yield return null is not a good use of a coroutine – Anton Mihaylov Jun 18 '19 at 17:59
  • Honest question: what is the real difference between `yield return null;` and `yield return new WaitForEndOfFrame();`? From what I've read, they do the same thing, but I couldn't find a clear, in-depth answer anywhere. – Mravec Jun 18 '19 at 19:11
  • 1
    @pravymravec Those both execute once per frame, but at different points in the update loop. You can find detailed info here: https://docs.unity3d.com/Manual/ExecutionOrder.html – rutter Jun 18 '19 at 20:37
  • `Firstly, your Coroutine doesn't have anything worth making it a coroutine` - That is not true ... OP can stick to `yield return null;` if he wants to .. it is the most used case. It simply yields somewhere in the `Game Logic` execution block, as rutter mentioned correctly. You **only** use `WaitForEndOfFrame` in special cases where you want it to wait with some execution until everything else is done for that frame. – derHugo Jun 19 '19 at 05:00
  • Oh, my bad - I didn't realize `yield return null` would execute in the same speed as updates. I've always used WaitForEndOfFrame() when I wanted to simulate Update-behaviour in coroutine loops. Thanks, I'll edit the answer and remove this part AND I learned something I previously misunderstood about coroutines! What a day! – Fredrik Schön Jun 19 '19 at 14:07
  • `would execute in accordance with the update loop*` "same speed" is disinformative. – Fredrik Schön Jun 19 '19 at 14:26
  • @FredrikSchön nope, didnt work... Making the function a coroutine just stops everything, still tryna know why – Sarm Jun 19 '19 at 19:54
  • You will need to call the function you converted to IEnumerator with a `StartCoroutine()` – Fredrik Schön Jun 20 '19 at 07:46