Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Wait 5 second after trigger is activated?

I am trying to wait five seconds after the trigger is met then after the five seconds is up i want to go to the next scene. The problem is that once the trigger is met the it automatically goes to the next scene.

What I have tried

using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour {


IEnumerator WaitAndDie()
{
    yield return new WaitForSeconds(5);

}
void Update()
{

        StartCoroutine(WaitAndDie());         

}
void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        Update();     
        Application.LoadLevel("GameOverScene");
        return;
    }

}
}

I have also tried

using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour {


IEnumerator WaitAndDie()
{
    yield return new WaitForSeconds(5);

}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        StartCoroutine(WaitAndDie());         
        Application.LoadLevel("GameOverScene");
        return;
    }

}
}
like image 490
hue manny Avatar asked Mar 17 '23 02:03

hue manny


1 Answers

Only call Application.LoadLevel after yield return :).

IEnumerator WaitAndDie()
{
    yield return new WaitForSeconds(5);
    Application.LoadLevel("GameOverScene");
}

void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        StartCoroutine(WaitAndDie());         
        return;
    }

}
}
like image 172
Barış Çırıka Avatar answered Apr 01 '23 13:04

Barış Çırıka