c#为什么我的协程只执行一次而不管while循环的条件是什么?

时间:2017-06-05 04:25:51

标签: c# unity3d

我试图执行一个从游戏对象队列中销毁的协程,每次执行之间都有暂停,直到游戏对象的队列被清除,我已经尝试将它放在启动函数和更新函数中并且两者都给出了相同的结果,只运行一次。 coroutine在没有clearDestroyStack函数的情况下工作正常。

所有代码都在附加到队列中的游戏对象的脚本中。

这是电话

if (lightningListFilled) {
        StartCoroutine ("clearLighteningTargets");
        lightningListFilled = false;
    }
    else {
    }

以下是协程。

IEnumerator clearLighteningTargets(){
    while (lighteningDestroyList.Count > 0) {
        Debug.Log ("From clear: "+lighteningDestroyList.Count);
        refScript.destroyStack.Push (lighteningDestroyList.Dequeue ());
        refScript.clearDestroyStack ();

        yield return new WaitForSeconds (0.5f);
        Debug.Log ("From clear: "+lighteningDestroyList.Count);

    }
}

这是clearDestroyStack函数的代码

public void clearDestroyStack (){
    while (destroyStack.Count != 0) {
        GameObject ballToDestroy = destroyStack.Pop ();
        List<GameObject> ballToDestroyParents = ballToDestroy.GetComponent<MoveToCenter> ().parents;
        List<GameObject> ballToDestroyChildren = ballToDestroy.GetComponent<MoveToCenter> ().children;
        foreach (GameObject parent in ballToDestroyParents) {
            if (parent != null) {
                if (parent.GetComponent<MoveToCenter> ().children.Contains (ballToDestroy)) {
                    parent.GetComponent<MoveToCenter> ().children.Remove (ballToDestroy);
                }   
            }

        }
        foreach (GameObject child in ballToDestroyChildren) {
            if (child != null) {
                if (child.GetComponent<MoveToCenter> ().parents.Contains (ballToDestroy)) {
                    child.GetComponent<MoveToCenter> ().parents.Remove (ballToDestroy);
                }
            }

        }
        GameObject[] allBalls = GameObject.FindGameObjectsWithTag ("ball");


        foreach (GameObject ball in allBalls) {
            ball.GetComponent<MoveToCenter> ().isSafe = false;
        }
        Destroy (ballToDestroy);
        //startSafeScan ();
        //deleteUnsafeBalls ();
        //clearDestroyStack ();
    }   
}

1 个答案:

答案 0 :(得分:0)

如果它们所附着的物体被破坏,它们就会停止。