Using yield wait for seconds in my script, correctly

I am using this script to check when all my “brick” objects are destroyed and then it loads the next level.

function Update(){

    var bricks = GameObject.FindWithTag("brick"); //find gameobjects with the tag "brick" (you must therefore tag the enemies with "brick"

    if(bricks == null){ //if no gameobjects with the tag "brick" are found

	

    //change scene

	Application.LoadLevel(Application.loadedLevel + 1);

    }

}

What I want to do is have a delay when all the bricks are destroyed and display “stage complete” (or something like that) and play a sound. I have tried using yield WaitForSeconds (5); I placed it above application load level. But this does not work. I looked at the script reference but was left still confused. Can anyone help me to correctly place it in my script? I’m utter n00b at scripting, design is my strong point.

You cannot use yield in Update()! Instead of calling Application.LoadLevel, call a function which will yield WaitForSeconds(5) and then load the next level. Try it, if you still have trouble with it let me know!

I would modify the script as below. Just add the sound and the “stage complete” text!

 var countdown = false;

 function Update(){
    var bricks = GameObject.FindWithTag("brick");
    if(bricks == null && !countdown){
       countdown = true;
       NextLevel();
    }
 }

 function NextLevel(){
    yield WaitForSeconds (5);
    Application.LoadLevel(Application.loadedLevel + 1);
 }