- Home /
How can I delay Application.Loadlevel for a few seconds?
Hello. I am a beginner to Unity. My question is how do I delay my game moving to the game over scene for a few seconds, as I want a particle system to fully play out. After the enemy dies, the game instantly moves to the next scene (either You Win or You Lose). There is a massive explosion that happens when the enemy wins and I want this to play out. I have included my script (JavaScript). Thank you for any help! This is an edit as requested by fattie. (Sorry I missed your comment a few minutes ago.)
var enemyCharge: int; var HP: int; var enemyWins: Transform; var enemyPickup: Transform;
function OnGUI () { GUI.Label (Rect (10, 50, 100, 20), "Charges: " + enemyCharge); GUI.Label (Rect (10, 80, 100, 20), "Hit Points: " + HP); }
function OnTriggerEnter (collider: Collider) {
if(enemyCharge == 9) { //This is where the enemy wins
var tempenemyWins: Transform; tempenemyWins = Instantiate (enemyWins, transform.position, transform.rotation); Destroy (gameObject);
//Transition to lose screen. Put Invoke here
Invoke ("LoseScreen", 3); }
if(collider.gameObject.tag == "EnemyCharge") { var tempenemyPickup: Transform; tempenemyPickup = Instantiate (enemyPickup, transform.position, transform.rotation); enemyCharge ++; collider.gameObject.transform.position.y = 17; collider.gameObject.transform.position.x = Random.Range (-20,15); } if(collider.gameObject.tag == "Bullet") { HP --; } }
function Update () { if(HP == 0) {
//This is where the player wins
Destroy (gameObject);
//Transition to win screen
Application.LoadLevel(3); }
// Invoke function itself
function LoseScreen() { Application.LoadLevel(0); }
}
if you're just getting started, learn about the Invoke command. enjoy!
Answer by aldonaletto · Jul 15, 2012 at 05:06 AM
The easiest way is to call a coroutine that waits for the specified time before loading the next level - like this:
... function Update () { if(HP == 0) { //This is where the player wins Destroy (gameObject); LoadLevel(3, 2.5); // call the coroutine } }
function LoadLevel(level: int, delay: float){ // wait the delay time... yield WaitForSeconds(delay); // then load the level: Application.LoadLevel(level); }
Thanks for responding! However I cannot compile as function Update() cannot have a coroutine.
I also tried to copy and paste your code, but the error says "No Appropriate version of 'Unity.Application.LoadLevel' for this argument list. I am not really sure what that means.
You have to have pasted his coroutine wrong - Update is not a coroutine in @aldonaletto's answer
I urge you to simply try using "Invoke".
It is a very very very simple command, unity aded for exactly this purpose! Hope it helps!
Hi. Perhaps I am not doing something right with the Invoke command? The game would not transition to the lose screen when the enemy won. I am still learning program$$anonymous$$g so maybe I did something wrong with Invoke.
var enemyCharge: int; var HP: int; var enemyWins: Transform; var enemyPickup: Transform;
function OnGUI () { GUI.Label (Rect (10, 50, 100, 20), "Charges: " + enemyCharge); GUI.Label (Rect (10, 80, 100, 20), "Hit Points: " + HP); }
function OnTriggerEnter (collider: Collider) {
if(enemyCharge == 9) { //This is where the enemy wins
var tempenemyWins: Transform; tempenemyWins = Instantiate (enemyWins, transform.position, transform.rotation); Destroy (gameObject);
//Transition to lose screen. Put Invoke here
Invoke("LoseScreen", 2.5);
}
if(collider.gameObject.tag == "EnemyCharge") { var tempenemyPickup: Transform; tempenemyPickup = Instantiate (enemyPickup, transform.position, transform.rotation); enemyCharge ++; collider.gameObject.transform.position.y = 17; collider.gameObject.transform.position.x = Random.Range (-20,15); } if(collider.gameObject.tag == "Bullet") { HP --; } }
function Update () { if(HP == 0) { //This is where the player wins
Destroy (gameObject);
//Transition to win screen
Application.LoadLevel(3); }
} //The invoke function itself
function LoseScreen() { Application.LoadLevel(0); }
Answer by delstrega · Jul 15, 2012 at 05:08 AM
Add
yield WaitForSeconds (2.5);
before the Application.LoadLevel(...) where "2.5" is the time in seconds to wait.
This will exit the execution of your OnTriggerEnter, wait for 2.5 seconds, then reenter OnTriggerEnter in exactly the position it left off of. This means it will execute everything after your "yield WaitForSeconds(2.5)" when it returns.
Thanks for responding! However, when is add yield WaitForSeconds (2.5); before Application.LoadLevel(0); the game does not transition to the lose screen at all.
$$anonymous$$ight have something to do with OnTriggerEnter not liking to be a coroutine. Try Aldo Netto's approach which is basically the same - difference being an extra function created.
Your answer
Follow this Question
Related Questions
Have a function wait unitl a boolean is false 1 Answer
restart level when dead with delay 3 Answers
Coroutine not running after yield return new WaitForSeconds 3 Answers
Create a delay between function executions 1 Answer
Yield only working once? 0 Answers