# WaitforSeconds() not working as it should

I searched for using WaitforSeconds and used as it was mentioned(using a return type of IEnumeration and using coroutines instead of update). but it did not work. Initially it showed Waitfor Seconds and IEnumerator were not "present in the current context". I had to r-install unity to get it fixed but this problem still remains. The following is my code. Am I using WaitforSeconds in correct way? Is it the 'if' code block that ruin my complete work(I mean have I used it in wrong place)?

using UnityEngine;
using System.Collections;

public class EnemyCreeator : MonoBehaviour {
public float xmin,xmax,zmin,zmax;
public GameObject enemy;
public float spawnWait,StartWait,waveWait;
public int turretCount;
public int enemyCount;
int i=0;
void Update() {
StartCoroutine (TurretWaves());
}
IEnumerator TurretWaves() {

while (true) {
Vector3 pos=new Vector3(Random.Range (xmin,xmax),0.5f,Random.Range(zmin,zmax));
yield return new WaitForSeconds(StartWait);
while(i<turretCount) {
//Debug.Log ("The vaue of game time in spawnwaiting is: "+Time.time);
Instantiate (enemy, pos, Quaternion.identity);
enemyCount++;
i++;
//Debug.Log ("value of i in loop is: "+i);
//Debug.Log ("The vaue of game time is: "+Time.time);
yield return new WaitForSeconds (spawnWait);
}
//Debug.Log("cHECKing Before WAVE WAIT(value of time )is: "+Time.time);
yield return new WaitForSeconds(waveWait);
if(i>=turretCount) {
i=0;
}
//Debug.Log("cHECKing AFTER WAVE WAIT and value of time is: "+Time.time);
//Debug.Log ("value of i outside the while loop is: "+i);
}
}
}


The code needs to wait until the spawnWait before spawning each turret and wait till the wavewait before spawning the next wave. even though the Startwait works fine, I still am unable to find the problem with others...

• There are three yield returns. I suggest cleaning up your code. Good practice is each function/method performing a specific action. – SanSolo Jun 24 '15 at 5:55
• Your while loop is while(true) . If it has to execute every time, that while is completely unnecessary. – SanSolo Jun 24 '15 at 5:59
• you were running a while-true loop on every frame? just call the coroutine once in Awake() or Start() – selharxets Jun 24 '15 at 6:42

This bit:

void Update() {
StartCoroutine (TurretWaves());
}


Once you start a coroutine, it will continue processing automatically. If you call StartCoroutine twice, you will launch two independent coroutines. If you call StartCoroutine once per frame, you will start a new, fully independent coroutine on every frame.

By the time your game finishes running 50 frames, you will have 50 copies of TurretWaves running. This is probably not what you want or expect.

If you want 1 copy of TurretWaves, you will need to start it exactly once:

void Start() {
StartCoroutine (TurretWaves());
}


Usualy it's a good practise to use StartCoroutine into the Start(), you're "starting" a coroutine on every update!!

Here can find some examples on using StartCoroutine.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void Start() {
print("Starting " + Time.time);
StartCoroutine(WaitAndPrint(2.0F));
print("Before WaitAndPrint Finishes " + Time.time);
}
IEnumerator WaitAndPrint(float waitTime) {
yield return new WaitForSeconds(waitTime);
print("WaitAndPrint " + Time.time);
}
}

• Is it that when the code ''Waits for Seconds()" it continuously run the code-blocks just before the WaitforSeconds() line?While using those debud.log code in comment i found that.Am i correct? Bcoz i think it can be exploited by me if it happens to be correct. – Abhishek Sinha Jun 24 '15 at 16:51
• @SanSolo- i used a whil loop every frame in order to instantiate the enemy turret then check for their number every frame. Is it worng? I know it requires quite a bit of processing power but its seems working. – Abhishek Sinha Jun 24 '15 at 16:53
• Sorry for the spelling mistakes – Abhishek Sinha Jun 24 '15 at 17:06
• @AbhishekSinha if true is the condition for while, it means that block is always executed. So i don't see why you need it. – SanSolo Jun 24 '15 at 17:53
• Sorry actually i actually mistyped my code...the WaitforSeconds(StartWait) should be above the while(true) statement.This will allow only a single start wait while multiple spawn and wave wait...@SanSolo – Abhishek Sinha Jun 26 '15 at 8:49