Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Delay instantiate prefab inside for loop

Discussion in 'Scripting' started by mzaasya, Apr 12, 2020.

  1. mzaasya

    mzaasya

    Joined:
    Dec 18, 2019
    Posts:
    5
    I want to clone the obstacles and place it randomly within a stage with the gap of 10 z position, so I used instantiate like this:

    Code (CSharp):
    1. public GameObject obstacle;
    2.  
    3.     void Start()
    4.     {
    5.         obstacle = GameObject.Find("Obstacle");
    6.         StartSpawn();
    7.     }
    8.  
    9.     private void StartSpawn()
    10.     {
    11.         int distance = -490;
    12.         for (; ; )
    13.         {
    14.             if (distance <= 0)
    15.             {
    16.                 Vector3 position = new Vector3(Random.Range(-3, 3), 1, distance);
    17.                 distance += 10;
    18.                 Instantiate(obstacle, position, Quaternion.identity);
    19.             }
    20.             else
    21.                 break;
    22.         }
    23.     }
    But when I run it my laptop freeze and then an error message pop up, it says "Out of Memory", so I think I should delay it but I have no idea how to do that.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,871
    The freezing and running out of memory strongly indicates that there is an infinite loop in your code. I can't see it, but the way you're doing your loop handling is pretty convoluted. Try cleaning up your for loop and see if that helps:

    Code (CSharp):
    1.  
    2.             private void StartSpawn() {
    3.                 for (int distance = -490; distance <= 0; distance += 10) {
    4.                     Vector3 position = new Vector3(Random.Range(-3, 3), 1, distance);
    5.                     Instantiate(obstacle, position, Quaternion.identity);
    6.                 }
    7.             }
    8.  
    Alternatively, if you do want to add a delay between spawning each item, you can use a coroutine:

    Code (CSharp):
    1.         void Start() {
    2.             StartCoroutine(StartSpawn())
    3.         }
    4.  
    5.         private IEnumerator StartSpawn() {
    6.             for (int distance = -490; distance <= 0; distance += 10) {
    7.                 Vector3 position = new Vector3(Random.Range(-3, 3), 1, distance);
    8.                 Instantiate(obstacle, position, Quaternion.identity);
    9.                 yield return new WaitForSeconds(.1f);
    10.             }
    11.         }
     
    Last edited: Apr 12, 2020
    mzaasya likes this.
  3. mzaasya

    mzaasya

    Joined:
    Dec 18, 2019
    Posts:
    5
    Thanks for your answer sir, I already used for loop like that in my first try. I thought I handle the loop wrongly so I tried to use another method of looping, that's why my loop looks like that.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,871
    Is it possible there is another piece of code in your game causing the issue instead? Try commenting out the code in this file and see if the issue persists.
     
  5. mzaasya

    mzaasya

    Joined:
    Dec 18, 2019
    Posts:
    5
    Thanks, it solved with IEnumerator as you said, but I should put WaitForSeconds at the first line inside for loop, if not it still freezes.

    Code (CSharp):
    1. IEnumerator StartSpawn()
    2.     {
    3.         for (int distance = -480; distance <= 0; distance += 10)
    4.         {
    5.             yield return new WaitForSeconds(.1f);
    6.             Vector3 position = new Vector3(Random.Range(-5, 5), 1, distance);
    7.             Instantiate(obstacle, position, Quaternion.identity);
    8.         }
    9.     }
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,871
    That's really strange! How complicated is your "obstacle" prefab?
     
  7. mzaasya

    mzaasya

    Joined:
    Dec 18, 2019
    Posts:
    5
    it's really simple cube from unity default 3D Object + default material just for coloring XD
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,871
    Something strange is surely going on then! Are you on a really old laptop? Instantiating ~50 objects in a single frame is usually not amazing performance wise but it certainly shouldn't completely freeze the game for substantial time or make the game run out of memory o_O
     
  9. mzaasya

    mzaasya

    Joined:
    Dec 18, 2019
    Posts:
    5
    Not really old, it's used core i5 8th gen with 8gb ram. Maybe the delay code didn't work properly when i put it on the bottom line