Search Unity

Feedback Unity memory overflow - When switching between foreach loops and for loops

Discussion in 'Editor & General Support' started by darkshoot, May 28, 2019.

  1. darkshoot

    darkshoot

    Joined:
    Oct 29, 2018
    Posts:
    1
    Hi, I was using some for loops and foreach loops in one of my scripts. I noticed when I was switching from checking using foreach to using for loops, Unity became unresponsive, and the memory usage was going up in the task manager.

    Here is the code cutout with foreach:
    Code (CSharp):
    1.  if (Player.transform.position.x > Backgrounds[0].transform.position.x + 1f && !hasSpawned)
    2.         {
    3.             foreach (GameObject o in Backgrounds)
    4.             {
    5.                 if (o.transform.position.x != Backgrounds[0].transform.position.x)
    6.                 {
    7.                     SpawnCounter = 1f;
    8.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x + 19.15f, 0)));
    9.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x + (19.15f * 2), 0)));
    10.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x + 19.181023f, 0)));
    11.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x + (19.181023f * 2), 0)));
    12.                     hasSpawned = true;
    13.                 }
    14.             }
    15.         }
    16.  
    17.         else if (Player.transform.position.x < Backgrounds[0].transform.position.x - 1f && !hasSpawned)
    18.         {
    19.             foreach (GameObject o in Backgrounds)
    20.             {
    21.                 if (o.transform.position.x != Backgrounds[0].transform.position.x)
    22.                 {
    23.                     SpawnCounter = 1f;
    24.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x - 19.15f, 0)));
    25.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x - (19.15f * 2), 0)));
    26.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x - 19.181023f, 0)));
    27.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x - (19.181023f * 2), 0)));
    28.                     hasSpawned = true;
    29.                 }
    30.             }
    31.         }
    Here is the code with for loops:
    Code (CSharp):
    1. if (Player.transform.position.x > Backgrounds[0].transform.position.x + 1f && !hasSpawned)
    2.         {
    3.             for(int i = 0; i < Backgrounds.Count; i++)
    4.             {
    5.                 if (Backgrounds[i].transform.position.x != Backgrounds[0].transform.position.x)
    6.                 {
    7.                     SpawnCounter = 1f;
    8.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x + 19.15f, 0)));
    9.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x + (19.15f * 2), 0)));
    10.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x + 19.181023f, 0)));
    11.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x + (19.181023f * 2), 0)));
    12.                     hasSpawned = true;
    13.                 }
    14.             }
    15.         }
    16.         else if (Player.transform.position.x < Backgrounds[0].transform.position.x - 1f && !hasSpawned)
    17.         {
    18.             for (int i = 0; i < Backgrounds.Count; i++)
    19.             {
    20.                 if (Backgrounds[i].transform.position.x != Backgrounds[0].transform.position.x)
    21.                 {
    22.                     SpawnCounter = 1f;
    23.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x - 19.15f, 0)));
    24.                     StartCoroutine(AddBackground(Background, new Vector3(Backgrounds[0].transform.position.x - (19.15f * 2), 0)));
    25.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x - 19.181023f, 0)));
    26.                     StartCoroutine(AddGrass(Grass, new Vector3(Grasses[0].transform.position.x - (19.181023f * 2), 0)));
    27.                     hasSpawned = true;
    28.                 }
    29.             }
    30.         }
    Any help would be appreciated.
    I also uploaded the entire script as a seperate file.
     

    Attached Files:

  2. jankovalsky

    jankovalsky

    Joined:
    May 31, 2017
    Posts:
    14
    Because in for-loop the condition is check in every iteration, and as you are increasing Backgrounds.Count it will go forever.
    Foreach creates copy of an array so its not affected.