C# IEnumerator is unchangable

Kuniq

i have a trouble with code below. What it does it builds a single line of road, one element after another.

currentLength = Random.Range(maxFloorLength - 2, maxFloorLength);
    List<GameObject> tempList = new List<GameObject>();

    for (int i = 0; i < currentLength; ++i)
    {
        foreach (GameObject g in pooledFloor)
        {
            if (!g.activeInHierarchy)
            {
                g.SetActive(true);
                g.transform.position = startPoint + buildDirection * i;
                g.transform.DOScaleY(5, 0.25f).Play();
                lastFloorPositions.Add(g.transform.position);
                tempList.Add(g);
                break;
            }
        }
        yield return new WaitForSeconds(0.05f);
    }

While this works as expected, when i change this to:

    currentLength = Random.Range(maxFloorLength - 2, maxFloorLength);
    List<GameObject> tempList = new List<GameObject>();

    for (int i = 0; i < currentLength; ++i)
    {
        foreach (GameObject g in pooledFloor)
        {
            if (!g.activeInHierarchy)
            {
                g.transform.position = startPoint + buildDirection * i;
                lastFloorPositions.Add(g.transform.position);
                tempList.Add(g);
                break;
            }
        }
    }

    foreach (GameObject g in tempList)
    {
        g.SetActive(true);
        g.transform.DOScaleY(5, 0.25f).Play();
        yield return new WaitForSeconds(0.05f);
    }

all it does is create ONE part of road (the one that should be at the end of tempList list). What i want is that script adds all the prefabs to list BEFORE doing something else with them, so i can add few thing in between. I tried to change it to void function, and calling some of this code as ienumerator, changing loop types etc. but nothing seems to work and i have no clue why is it not working. I checked with Debug.Log whether it still finds more than 1 prefab, and it does, but it only activates and scales ONE of them. I bet it's something obvious, but i just can't see it :/

How it looks:

How its working

How it should be looking:

How it should look

Forgot to mention - its all inside of IEnumerator

Umair M

In first code when you call g.SetActive(true);, in next iteration of for loop the if condition for the same object wont be true so it will evaluate for next inactive object and add it to tempList.

In second case for all iteration of for loop it keeps evaluating for same object g (the first inactive object on pooledFloor) because it is not being activated.

To achieve your expected behaviour, I would suggest you to use for-loop instead of for-each on pooledFloor and remove the object from the list if its inactive.

something like this:

for (int i = 0; i < currentLength; ++i)
{
    for (int j = 0; j < pooledFloor.Count; j++) 
    {
        GameObject g = pooledFloor[j];
        if (!g.activeInHierarchy)
        {
            g.transform.position = startPoint + buildDirection * i;
            lastFloorPositions.Add(g.transform.position);
            tempList.Add(g);
            pooledFloor.Remove(g);  // Removing it from list would solve the problem.
            break;
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C#: Custom IEnumerator?

From Dev

IEnumerator IEnumerable vb to C#

From Dev

Are deterministically unchangable Actions, and Funcs Inlined by the JIT?

From Dev

Windows Phone 8 ApplicationBar expanded unchangable transparancy

From Dev

C# IEnumerator with foreach loops, best design pattern?

From Dev

C# Npgsql connection error: Npgsql.NpgsqlConnection doesn't implement interface System.Collections.IEnumerator

From Dev

Randomizing order of IEnumerator

From Dev

IEnumerator.MoveNext() implementation?

From Dev

Modifying an IEnumerator after declaration

From Dev

Custom IEnumerator in F#

From Dev

Reverse the behavior of `IEnumerator`

From Dev

How does int map to IEnumerator<int> when implementing IEnumerator interface?

From Dev

F#: Downcast seq to IEnumerator

From Dev

IEnumerator yield return type error

From Dev

Implementing IEnumerator/ IEnumerable with IDispose error

From Dev

What is the reason implementing IEnumerable and IEnumerator

From Dev

Converting List<string> to IEnumerator<string>

From Dev

IEnumerator yield return type error

From Dev

return type of public IEnumerator GetEnumerator()?

From Dev

IEnumerator does not implement interface IEnumerable

From Dev

Why must IEnumerator<T> GetEnumerator() and GetEnumerator() be implemented?

From Dev

When should I separately implement IEnumerator<T>?

From Dev

Why is IEnumerable<T> necessary when there is IEnumerator<T>?

From Dev

Why doesn't the following work? (IEnumerable/ IEnumerator)

From Dev

The name IEnumerator does not exist in current context

From Dev

IEnumerable<T> and IEnumerator - some clarification please

From Dev

How does GC work with IEnumerator and yield?

From Dev

Why IEnumerator.MoveNext is called before GetCurrent?

From Dev

Implementation of IEnumerator without using 'yield return'

Related Related

  1. 1

    C#: Custom IEnumerator?

  2. 2

    IEnumerator IEnumerable vb to C#

  3. 3

    Are deterministically unchangable Actions, and Funcs Inlined by the JIT?

  4. 4

    Windows Phone 8 ApplicationBar expanded unchangable transparancy

  5. 5

    C# IEnumerator with foreach loops, best design pattern?

  6. 6

    C# Npgsql connection error: Npgsql.NpgsqlConnection doesn't implement interface System.Collections.IEnumerator

  7. 7

    Randomizing order of IEnumerator

  8. 8

    IEnumerator.MoveNext() implementation?

  9. 9

    Modifying an IEnumerator after declaration

  10. 10

    Custom IEnumerator in F#

  11. 11

    Reverse the behavior of `IEnumerator`

  12. 12

    How does int map to IEnumerator<int> when implementing IEnumerator interface?

  13. 13

    F#: Downcast seq to IEnumerator

  14. 14

    IEnumerator yield return type error

  15. 15

    Implementing IEnumerator/ IEnumerable with IDispose error

  16. 16

    What is the reason implementing IEnumerable and IEnumerator

  17. 17

    Converting List<string> to IEnumerator<string>

  18. 18

    IEnumerator yield return type error

  19. 19

    return type of public IEnumerator GetEnumerator()?

  20. 20

    IEnumerator does not implement interface IEnumerable

  21. 21

    Why must IEnumerator<T> GetEnumerator() and GetEnumerator() be implemented?

  22. 22

    When should I separately implement IEnumerator<T>?

  23. 23

    Why is IEnumerable<T> necessary when there is IEnumerator<T>?

  24. 24

    Why doesn't the following work? (IEnumerable/ IEnumerator)

  25. 25

    The name IEnumerator does not exist in current context

  26. 26

    IEnumerable<T> and IEnumerator - some clarification please

  27. 27

    How does GC work with IEnumerator and yield?

  28. 28

    Why IEnumerator.MoveNext is called before GetCurrent?

  29. 29

    Implementation of IEnumerator without using 'yield return'

HotTag

Archive