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.
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?
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
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