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);
}