【问题标题】:Problems with IEnumerator WaitForSeconds in UnityUnity 中 IEnumerator WaitForSeconds 的问题
【发布时间】:2014-06-06 19:27:59
【问题描述】:

所以我试图在一个while循环中消灭敌人,中间等待1秒(他们会让等待更难吗??)问题是,所有敌人都被同时消灭,他们没有等待WaitForSEconds。 在我的 while 循环中,我通过标签调用每个敌人,标签从 Enemy1 到 Enemy5。 这是我的代码。

void OnTriggerEnter(Collider otherObject)
{


    int i=1;
    while (i<=numenemies)
    {
        string tag="Enemy"+i;
        destroyenemy=GameObject.FindGameObjectWithTag(tag);
        Destroy(destroyenemy);
        i++;
        StartCoroutine(DestroyWait ());


    }   
 }
 IEnumerator DestroyWait()
 {
   Debug.Log ("so far...");
   yield return new WaitForSeconds (1);
   Debug.Log ("so good");

 }

在我的调试日志中的控制台中,我得到 4 个“到目前为止...”,然后是 4 个“非常好”。它不等待 1 秒然后输出这么好。

我一直在阅读这方面的内容,而且很难将脚本暂停 1 秒钟!我做错了什么?

【问题讨论】:

  • @YuvalItzchakov 不,它会停止游戏

标签: c# unity3d wait ienumerator


【解决方案1】:

将所有代码移动到协程中:

void OnTriggerEnter(Collider otherObject)
{
    StartCoroutine(DestroyAllEnemies());
}

IEnumerator DestroyAllEnemies()
{
    for(int i = 1; i<=numenemies;i++)
    {
        string tag="Enemy"+i;
        destroyenemy=GameObject.FindGameObjectWithTag(tag);
        Destroy(destroyenemy);
        yield return new WaitForSeconds (1);
    }
 }

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 2022-10-05
    • 2019-06-19
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2015-06-14
    相关资源
    最近更新 更多