- Home /
 
Problem with yield waitForSeconds in for loop
Hi everyone! I am playing around with a script for my game that generates terrain features on my level! Everything works excellent so far, but when i tried to generate in "slow motion" to actually see how it works, nothing seemed to work. It doesn't generate a single object! If i take out the "yield WaitForSeconds(0.1);" everything works! If you know something or could help me i'd be greatfull, thanks :)
This is the function i use:
  function GenerateForests (forests , roomPerObject, obj, minForestSize, maxForestSize) { // Generate "forests" of objects in random locations
     
     
     for(var n = 0; n < forests; n++) {
             var forestWidth = Random.Range (maxForestSize , minForestSize);
             var forestLength = Random.Range (maxForestSize , minForestSize);
             
             var forestX = Random.Range (0 , mapSize);
             var forestZ = Random.Range (0 , mapSize);
             
             if ( (forestX + forestWidth) > mapSize) {
                     forestWidth -= ( (forestX + forestWidth) - mapSize);
             }
             
             if ( (forestZ + forestLength) > mapSize) {
                     forestLength -= ( (forestZ + forestLength) - mapSize);
             }
             
             var forestSize = (forestWidth * forestLength);
             
             var _height : float = 0;
             
             if (obj == tree) _height = 5;
             if (obj == mine) _height = 1.5;
             
             
             Debug.Log (n + " " + forestSize);
     
             for(var i = 1; i < (forestSize / roomPerObject ); i++) {
                 yield WaitForSeconds(0.1);
                 Instantiate (obj , Vector3( (forestX + (Random.Range (0 , forestWidth))), _height, (forestZ + (Random.Range (0 , forestLength)))), Quaternion.identity);
                             }
         }
     }
 
              Your answer
 
             Follow this Question
Related Questions
Audio loop after time 2 Answers
How can I tell Unity to wait to do something until after and animation has finished playing? 0 Answers
How can I tell Unity to wait to do something until after and animation has finished playing? 1 Answer
[Javascript] Creating waiting points for random movement with NavMesh Script 1 Answer
A node in a childnode? 1 Answer