Unity Editor is freezing due to coroutine

in lecture 93. Repeat Fire Coroutine, I remember following the lecture step by step and everything worked great. This time, I tried to repeat it but the whole unity Editor freezes every time I click on play. I have to force quit using TaskManager.

Here is a screenshots of my code

1

2

Any amount of help would be apricated.

Edit: I have realized the mistake. the “if statement” should not be wrapped with the “while(true) statement”, only the “GameObject laser” code.

it should be in the fire method right here

4

Hi Hue,

Welcome to our community! :slight_smile:

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime? How often does the Fire() method get called?

Hope this helps :slight_smile:


See also;

1 Like

Hi Nina,

I figured out the error, so previously, I wrapped the if (Input.GetButtonDown(“Fire1”)) in the while(true) statement and it looked something likes this

 IEnumerator FireRapidly()
    {
        while (true) {

            if (Input.GetButtonDown("Fire1"))
            {
                GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
                laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);


                yield return new WaitForSeconds(projectileFiringPeriod);
            }
        }

While the startCoroutine was only inside the Fire Method with no if statement and it look like this

private void Fire()
    {
       Firingcoroutine = StartCoroutine (FireRapidly());

        if(Input.GetButtonUp("Fire1"))
        {

            StopCoroutine(Firingcoroutine);

        }

This cause the Unity Editior to freeze and not respond the moment I clicked on play, thus I had to force quit using windows TaskManager

The solution was as shown the lecture is to take the
if (Input.GetButtonDown(“Fire1”)) and move it to the Fire method, then put the Firingcoroutine = StartCoroutine (FireRapidly()); inside of it and should look like this

 private void Fire()
    {

        if (Input.GetButtonDown("Fire1"))
        {

            Firingcoroutine = StartCoroutine(FireRapidly());
        }
        if(Input.GetButtonUp("Fire1"))
        {

            StopCoroutine(Firingcoroutine);

        }
    }

This was an oversight on my part and not a problem in the lecture, I apologize for that.

No need to apologise. Making mistakes and solving them is a big part of programming. :slight_smile:

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

Privacy & Terms