waitforseconds()后的语句不能统一工作
本文关键字:不能 工作 语句 waitforseconds | 更新日期: 2023-09-27 18:03:21
我也是unity和c#的新手。我想延迟一个函数,因此使用WaitForSeconds()
,但我的问题是,WaitForSeconds()
之后的语句没有执行,因此没有显示延迟。
代码如下:
public void GameOver(){
StartCoroutine (Load ());
Debug.Log("loadDelay");
}
IEnumerator Load(){
Debug.Log ("enum");
yield return new WaitForSeconds(3);
Debug.Log("waited");
}
控制台的输出显示:
enum
loadDelay
当GameOver()
和Debug.Log("waited");
同时被调用时,没有任何延迟
根本不执行。我真的不明白这个问题。如果我做错了什么,请解释一下。由于
调用GameOver()
的代码:
public class Collision : MonoBehaviour {
public GameObject explosion;
BgScroll bg;
void Start () {
bg = GetComponent<BgScroll> ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
if (col.gameObject.tag == "Enemy") {
//Debug.Log("destroyed");
explosion.renderer.sortingLayerName = "foreground";
Instantiate (explosion, transform.position, Quaternion.identity);
Destroy (gameObject);
Debug.Log("destroyed");
bg.GameOver();
}
}
}
根据Unity的WaitForSeconds文档,你做得对。当我试着运行你的代码时,它运行得很好。一定有其他问题,或者在"等待"之前可能有其他日志。控制台窗口的输出为
enumloadDelay
等
和明显的"waited"如果你的播放器正在运行,将在3秒后打印。