- Home /
How do you delay Application.LoadLevel ?
I am attempting to delay Application.LoadLevel so that a sound can finish playing in my game. This is the script I currently have set up
function Update ()
{
if (CoinsCollected == 3)
{
DelayRestart();
Application.LoadLevel(LevelToChangeTo);
}
}
function DelayRestart()
{
yield WaitForSeconds (3);
}
but Unity doesn't call the DelayRestart function and I was wondering if anyone knows what to do.
Answer by HarshadK · Aug 26, 2014 at 10:01 AM
You should put your Application.LoadLevel after your yield statement in your coroutine.
function Update ()
{
if (CoinsCollected == 3)
{
DelayRestart();
}
}
function DelayRestart()
{
yield WaitForSeconds (3);
Application.LoadLevel(LevelToChangeTo);
}
yield WaitForSeconds does not work at all before changing a scene.
Answer by Anderson_Antunes · Feb 01, 2015 at 10:33 PM
I have a simplier solution:
private float timer;
void Update(){
if (CoinsCollected == 3) {
timer = Time.deltaTime;
if (timer > 3)
Application.LoadLevel(LevelToChangeTo);
}
}
errr, no. That would load a new level if the player had collected 3 coins, and the last frame had taken more than 3 seconds to render...
I really wish that had worked but sadly it did not. Had pretty much no affect whatsoever.
Your answer
Follow this Question
Related Questions
Delaying through yield 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to implement a delay between each time my gun fires (using coroutines or otherwise)?... 2 Answers
How to reload a gun with delay ? 2 Answers
Looping t wo functions until a condition is not met? 1 Answer