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

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

我试过了什么

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

}
}

解决方法:

这应该工作

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;

    }

}
}
上一篇:javascript – 基于类更改的触发器功能


下一篇:如何避免MySQL中的循环触发器依赖