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.
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);
}
I have a simplier solution:
private float timer;
void Update(){
if (CoinsCollected == 3) {
timer = Time.deltaTime;
if (timer > 3)
Application.LoadLevel(LevelToChangeTo);
}
}