触发器激活后如何等待 5 秒?

How to Wait 5 second after trigger is activated?

我试图在触发触发后等待五秒,然后在五秒后我想转到下一个场景。问题是一旦满足触发器,它就会自动转到下一个场景。

我试过的

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

}
}

我也试过了

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

}
}

仅在 yield return 之后调用 Application.LoadLevel :).

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

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

}
}

这应该有效

using UnityEngine;
using System.Collections;

public class DestroyerScript : MonoBehaviour {


bool dead;

IEnumerator OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player") 
    {
        yield return new WaitForSeconds(5);
        Application.LoadLevel("GameOverScene");
        dead = true;
        return dead;

    }

}
}