首页 > 解决方案 > Unity Coroutine Yield 似乎每次都关闭协程

问题描述

嘿,所以我一直在研究这种随时间变化的损坏代码,我已经使用了许多在线资源,并且我有大量的尝试迭代,但它没有奏效,所以我来到这里。免责声明:我的对象在屈服时没有被破坏,它只是达到了产量,没有任何反应。

if(CurrentStatus == Status.Burning && burning==false){
burning=true;
    StartCoroutine (DamageOverTimeCoroutine(100, 5));
    Debug.Log("what");
    burning = false;
    //CurrentStatus = Status.Empty;

}

public IEnumerator DamageOverTimeCoroutine(float damageAmount, float duration){
        float amountDamaged = 0;
        float damagePerLoop = damageAmount / duration;
        while (amountDamaged < damageAmount) {
            int numInt = (int)Math.Ceiling(damagePerLoop);
            enemyCurrentHealth -= numInt;
            var clone = (GameObject) Instantiate(damageNumber, this.gameObject.transform.position, Quaternion.Euler(Vector3.zero));
            clone.GetComponent<FloatingNumbers> ().damageNumber = numInt;
            amountDamaged += damagePerLoop;
            yield return null;
            Debug.Log(amountDamaged);
        }
        if(amountDamaged>damageAmount){
            //StopCoroutine(DamageOverTimeCoroutine());
        }
        CurrentStatus = Status.Empty;
        burning=false;
    }

重申一下,它会造成损害,然后在收益回报时退出,我已经将它从 WaitSeconds 更改,只是为了看看它是否可以以任何其他方式工作,它不会。在 yield fire 之后我从来没有调试过。为什么会这样?

将不胜感激,谢谢

标签: c#unity3d

解决方案


想通了, var clone = (GameObject) Instantiate(damageNumber, this.gameObject.transform.position, Quaternion.Euler(Vector3.zero)); clone.GetComponent ().damageNumber = numInt;

行简短地循环它而没有引发错误。奇怪,但它现在有效。感谢输入的家伙


推荐阅读