Skip to main content
Fixed some code logic errors.
Source Link
LifGwaethrakindo
  • 1.4k
  • 2
  • 15
  • 27

You could put the ForEach inside the coroutine:

public class SomeObject : MonoBehaviour
{
    protected List<EdibleStuff> SomeItemsToConsume;

    private IEnumerator Consume()
    {
        this.SomeItemsToConsume
        .ForEach(item =>
        {
            WaitForSecondsWaitForSecondsRealtime wait = new WaitForSecondsRealtime(2);

            /// Process your item...

            yield return wait; 
        });
    }
}

Since what it was done in your StartConsuming method was that in your loop a 'n' number of coroutines (being 'n' the count of your list) were being started at the same frame. Having it inside a coroutine makes sure that the List moves to the next element once the WaitForSecondsRealTime's keepWaiting property returns true, which is not on the same frame (even if the time passed were 0).

Hope it helps.

You could put the ForEach inside the coroutine:

public class SomeObject : MonoBehaviour
{
    protected List<EdibleStuff> SomeItemsToConsume;

    private IEnumerator Consume()
    {
        this.SomeItemsToConsume
        .ForEach(item =>
        {
            WaitForSeconds wait = new WaitForSecondsRealtime(2);
            yield return wait; 
        });
    }
}

Since what it was done in your StartConsuming method was that in your loop a 'n' number of coroutines (being 'n' the count of your list) were being started at the same frame. Having it inside a coroutine makes sure that the List moves to the next element once the WaitForSecondsRealTime's keepWaiting property returns true, which is not on the same frame (even if the time passed were 0).

Hope it helps.

You could put the ForEach inside the coroutine:

public class SomeObject : MonoBehaviour
{
    protected List<EdibleStuff> SomeItemsToConsume;

    private IEnumerator Consume()
    {
        this.SomeItemsToConsume
        .ForEach(item =>
        {
            WaitForSecondsRealtime wait = new WaitForSecondsRealtime(2);

            /// Process your item...

            yield return wait; 
        });
    }
}

Since what it was done in your StartConsuming method was that in your loop a 'n' number of coroutines (being 'n' the count of your list) were being started at the same frame. Having it inside a coroutine makes sure that the List moves to the next element once the WaitForSecondsRealTime's keepWaiting property returns true, which is not on the same frame (even if the time passed were 0).

Hope it helps.

Source Link
LifGwaethrakindo
  • 1.4k
  • 2
  • 15
  • 27

You could put the ForEach inside the coroutine:

public class SomeObject : MonoBehaviour
{
    protected List<EdibleStuff> SomeItemsToConsume;

    private IEnumerator Consume()
    {
        this.SomeItemsToConsume
        .ForEach(item =>
        {
            WaitForSeconds wait = new WaitForSecondsRealtime(2);
            yield return wait; 
        });
    }
}

Since what it was done in your StartConsuming method was that in your loop a 'n' number of coroutines (being 'n' the count of your list) were being started at the same frame. Having it inside a coroutine makes sure that the List moves to the next element once the WaitForSecondsRealTime's keepWaiting property returns true, which is not on the same frame (even if the time passed were 0).

Hope it helps.