Delaying a Application.LoadLevel();

Im building a racing game where you race against the clock trying to cross the finish line before time runs out. The script I have is a Timer script that causes the car to explode (Unity Explosion Framework) if Time runs out. What I’m having trouble with is letting player see the explosion then have the game over level load. My problem is when the timer runs out Application.LoadLevel(4); executes before the explosion. I have verified the explosion works but I can’t seem to let the car explode Then load the game over level. This is my script.

private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;

// near the top, with your other var declarations:
var explosionPrefab : GameObject;
var explosionLife : float = 2;
var camera1 : Camera; 
var camera2 : Camera; 

var countDownSeconds : int;

function Start () { 
   camera1.enabled = true; 
   camera2.enabled = false; 
} 

function Update () {
    if (Input.GetKeyDown ("2")){ 
      camera1.enabled = false; 
      camera2.enabled = true; 
   } 
   if (Input.GetKeyDown ("1")){ 
      camera1.enabled = true; 
      camera2.enabled = false; 
   }    
}

function Awake() {
    startTime = Time.time;
}

function OnGUI () {
    //make sure that your time is based on when this script was first called
    //instead of when your game started
    var guiTime = Time.time - startTime;

    restSeconds = countDownSeconds - (guiTime);

    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 0) {
        print ("Time is Over");
        var expPos = transform.position;
        var exp: GameObject = Instantiate (explosionPrefab, expPos, Quaternion.identity);
        Destroy(exp, explosionLife);
        Destroy(gameObject);
        camera1.enabled = false;
        camera2.enabled = true;
// want to know how to delay the Application.LoadLevel(4);
        //Application.LoadLevel(4);
    }

    //display the timer
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60; 

    text = String.Format ("Time Left: {0:00}:{1:00}", displayMinutes, displaySeconds); 
    GUI.Box (new Rect (Screen.width-180, 60, 180, 40), text);
}

use Invoke to run a command after a certain time :
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Invoke.html

I tried this but it still isn’t working. and I have double checked that level “4” exists.

private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;

// near the top, with your other var declarations:
var explosionPrefab : GameObject;
var explosionLife : float = 2;
var camera1 : Camera; 
var camera2 : Camera; 

var countDownSeconds : int;

function Start () { 
   camera1.enabled = true; 
   camera2.enabled = false; 
} 

function Update () {
    if (Input.GetKeyDown ("2")){ 
      camera1.enabled = false; 
      camera2.enabled = true; 
   } 
   if (Input.GetKeyDown ("1")){ 
      camera1.enabled = true; 
      camera2.enabled = false; 
   }    
}

function Awake() {
    startTime = Time.time;
}

function OnGUI () {
    //make sure that your time is based on when this script was first called
    //instead of when your game started
    var guiTime = Time.time - startTime;

    restSeconds = countDownSeconds - (guiTime);

    //display messages or whatever here -->do stuff based on your timer
    if (restSeconds == 60) {
        print ("One Minute Left");
    }
    if (restSeconds == 0) {
        print ("Time is Over");
        var expPos = transform.position;
        var exp: GameObject = Instantiate (explosionPrefab, expPos, Quaternion.identity);
        Destroy(exp, explosionLife);
        Destroy(gameObject);
        camera1.enabled = false;
        camera2.enabled = true;
        Invoke("gameover", 5);
    }

    //display the timer
    roundedRestSeconds = Mathf.CeilToInt(restSeconds);
    displaySeconds = roundedRestSeconds % 60;
    displayMinutes = roundedRestSeconds / 60; 

    text = String.Format ("Time Left: {0:00}:{1:00}", displayMinutes, displaySeconds); 
    GUI.Box (new Rect (Screen.width-180, 60, 180, 40), text);
}

function gameover()
{
	Application.LoadLevel(4);
}

1 problem : you don’t want to run the command in the OnGUI function since that gets called many times a second or find a way to have it called just once.
2 are you getting any errors ?

how can I make sure that the gameover level loads after the timer runs out and after the explosion? and no i’m not getting any errors

function gameover()
{
yield WaitForSeconds (5);
Application.LoadLevel(4);
}

That didn’t work but I figured out why it didn’t. Because i have the Destroy.gameObject in my script when the car blows up the script went with it. gosh I feel stupid. I destroyed an object and wondered why It didn’t work. Duh!