Skip to main content
1 of 5

why it's always difficult to understand coroutine?

I have a coroutine which I called from the update on every click.

like this

void Update () {
            if (Input.GetMouseButtonDown (0)) {
                AddTrunk ();
                if (!isRunning) {
                    StartCoroutine (BranchOutAnimation ());
                }
            }
        }

and I don't know the problem but I am not able to get proper output.

Here is the code for coroutine,

IEnumerator BranchOutAnimation(){
            while (true) {
                isRunning = true;
                iTween.MoveTo (trunkList [index], new Vector3 (trunkList [index].transform.position.x - 5f, trunkList [index].transform.position.y, trunkList [index].transform.position.z), 0.3f);
                yield return new WaitForSeconds (0.3f);
                Destroy (trunkList [index]);
                trunkList.RemoveAt (index);
                transform.position = new Vector3 (transform.position.x, transform.position.y - treeHeight, transform.position.z);
                isRunning = false;
                yield break;
            }
        }

now here is the flow what I want first AddTrunk method call then it run move animation for 0.3 seconds and after 0.3 seconds it calls destroy and then it changes transform.position but in my case before completing animation it runs transform.animation and because of that problem it first moves my tree down and then does animation. now tell me or guide me what I did wrong?