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

标签 c# triggers unity3d

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

我尝试过的

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

}
}

关于c# - 触发器激活后如何等待 5 秒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28404808/

相关文章:

sql-server - 在 SQL Server 中添加两个列值来填充第三列,这可以在没有触发器/存储过程的情况下完成吗?

ios Unity 3d 集成到 native ios 代码中

c# - 将 Unity 3D 与 ZXing 集成

c# - ASP.NET GridView 列删除

c# - 将正确的 Gcode 字符串发送到串行端口?

javascript - Google 脚本项目触发器未运行?

unity3d - 如何使编辑器句柄可选择以显示属性检查器窗口

c# - 当 allow null 为真时,为什么默认值不插入列中?

c# - 如何在 asp.net 中实现 SSL

postgresql - 在触发之前获取内部静态数据的最佳方法