Coroutine and Update()

Hi all,

I am still working on the TopDownShooter and although I asked about the coroutines before I am having difficulties implement this method.

I want to make some explosions when the boss-enemy ship gets destroyed. But I want to implement a series of explosions, like a second delay in between and in different spots of the ship. I have created an explosion prefab and referenced it on the boss-enemy script as well as an array of transforms. I wrote the following code.,

 void BigBossExplode()
    {
        if (bigBossHealth <= 0)
        {
            bigBossExploding = StartCoroutine(BigBossBoom());
        
        }
    }

with;


    IEnumerator BigBossBoom()
    {
       
            for (int i = 0; i < expolsionPositions.Length; i++)
            {

                GameObject exp = Instantiate(explosionBigBoss, expolsionPositions[i].transform.position, expolsionPositions[i].transform.rotation) as GameObject;
                yield return new WaitForSeconds(1);
                Destroy(exp, 2f);

                

            }


           // yield return new WaitForSeconds(2);
            Destroy(gameObject);
        }
    

and the BigBossExplode() method is called inside Update().

Now my problem is that the explosion game object gets called a million times. What I want is that the explosion instantiates once and once again on the other designated spot after a second and so on but now it is like that it is called a bunch of times on the first spot and again a bunch of times on the other spot after a second and so on.

What I thought might be wrong is that since the update is called every frame and the condition for the coroutine to start is met after the bigBossHealth hits the 0 mark, the coroutine is called many times which results in a million instantiated explosion game object. But I couldn’t figure out a workaround.

What am I missing?
Thanx in advance…=)

Hi cnkrtn,

Where does BigBossExplode() get called? If it’s in Update(), the if-condition is not sufficient because it does not take into consideration whether the coroutine was already started. From what I see in the method, your solution is almost complete. There is just a little detail missing.

if you call to courotin in the update you can use bulian of true or false in the start its false and when you ennterd to corotin function turn to true (in the if you put if(bigbboss<=0&&is false)and you enter the function and turn it to true and after second of yeild return you turn it back to false

Thanx… A boolian solved the problem…=) Cheers…=)

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms