简体   繁体   中英

c# why is my coroutine Executing only once regardless of condition for while loop?

I'm trying to execute a coroutine which destroys from a queue of gameobjects with a pause in between each execution until the Queue of game objects has been cleared, I have tried putting it in the start function and the update function and both give the same results of it only running once. coroutine works fine without clearDestroyStack function.

All the code is in a script attached to the game objects that are in the queue.

This is the call

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

The following is the coroutine.

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

    }
}

this is the code for the clearDestroyStack function

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

如果协程停止,它们所附着的对象将被破坏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM