Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by emonegarand · Jan 14, 2017 at 11:14 PM · c#freeze

Game freezes up for a second after executing code, how do I fix this?

Hello, I created a script to have a boss character fire a laser using the line renderer, it works well enough for my purposes but to get the effect my code is kind of sloppy and during part of the code execution towards the end the game will freeze for a split second before resuming. How can I fix or trim down my code to prevent this?

 public class anemoneLaser : MonoBehaviour {
 
     public BossBehavior bossBehavior;
 
     public float laserWidth;
     public float laserTimer;
     public float startTimer;
     private float nextShot;
     private float nextStartUp;
     public bool laserStarting;
     public bool laserReady;
     public bool laserOff;
     public float minWidth = 0;
     public float maxWidth = 2.3f;
     LineRenderer redLaser;
     ParticleSystem laserSparks;
 
     private AudioSource laserCharge;
     private AudioSource laserShot;
 
     // Use this for initialization
     void Start ()
     {
         GameObject bossBehaviorObject = GameObject.FindWithTag("Boss");
         if (bossBehaviorObject != null)
         {
             bossBehavior = bossBehaviorObject.GetComponent<BossBehavior>();
         }
         if (bossBehavior == null)
         {
             Debug.Log("Cannot find 'BossBehavior' script");
         }
 
         AudioSource[] laserSounds = GetComponents<AudioSource>();
         laserCharge = laserSounds[0];
         laserShot = laserSounds[1];
 
         laserTimer = nextShot;
         startTimer = nextStartUp;
 
         laserWidth = 0;
 
         redLaser = GetComponent<LineRenderer>();
         SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
         redLaser.sortingLayerID = spriteRenderer.sortingLayerID;
         redLaser.sortingOrder = spriteRenderer.sortingOrder;
         redLaser.widthMultiplier = laserWidth;
 
         laserSparks = GetComponentInChildren<ParticleSystem>();
         laserSparks.Pause();
     }
     
     // Update is called once per frame
     void Update ()
     {
         redLaser = GetComponent<LineRenderer>();
         redLaser.widthMultiplier = laserWidth;
 
         if (laserWidth < 0)
         {
             laserWidth = 0;
         }
         // Starting countdown for laser
         if (laserStarting)
         {
             laserOff = false;
             startTimer -= Time.deltaTime;
         }
 
         if (bossBehavior.bossActive && laserStarting && startTimer < 0 && !laserOff)
         {
             laserWidth = 0.1f; //sets laser to a minimum width so player knows its about to go off
             laserStarting = false;
             laserCharge.Play();
             laserSparks.Play();
             StartCoroutine(PrepareToFire());   
         }
         //Laser ready to fire
         if (laserReady)
         {
             laserTimer -= Time.deltaTime;
         }
 
         if (bossBehavior.bossActive && laserReady && laserTimer < 0 && !laserOff)
         {
             
             StartCoroutine(FirinMahLaser());
         }
 
     }
     IEnumerator PrepareToFire()
     {
         yield return new WaitForSeconds(0.8f);
         Debug.Log("PrepareToFire()");
         startTimer = nextStartUp;
         laserReady = true;
         
     }
     // Increases width of laser to maxWidth value before finishing and going into Cool Down
     IEnumerator FirinMahLaser()
     {
         //laserShot.Play(); //Not sure where to place this yet so it doesn't play more then once
         yield return new WaitForSeconds(3.5f);
         laserTimer = nextShot;
         
         if (laserWidth < maxWidth && !laserOff)
         {
             Debug.Log("Adding Width");
             laserWidth += 0.1f;
         }
         else if (laserWidth >= maxWidth && !laserOff)
         {
             yield return new WaitForSeconds(2.0f);
             Debug.Log("Laser Width at Max");
             StartCoroutine(CoolingOff());
         }
 
     }
     //Called to reduce width and turn off laser then move on to prepare for the next firing
     IEnumerator CoolingOff()
     {
         while (laserWidth > minWidth && !laserOff)
         {
             Debug.Log("Cooling off WHILE loop");
             laserWidth -= 0.01f;
         }
         if(laserWidth <= minWidth && !laserOff)
         {
             
             Debug.Log("WHILE loop broken");
             laserSparks.Stop();
             laserOff = true;
             laserStarting = false;
             laserReady = false;
             yield return new WaitForSeconds(10.0f);
             StartCoroutine(PrepNext());
         }
     }
     //Prepare for next laser
     IEnumerator PrepNext()
     {
         Debug.Log("PrepNext()");
         
         laserWidth = 0;
         laserTimer = nextShot;
         startTimer = nextStartUp;
         yield return new WaitForSeconds(2.0f);
         laserStarting = true;
     }
    //TODO Add collision to laser
 }

The freeze up happens after the while loop is broken, maybe that's what is creating the lag? Any help would be appreciated. Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by WeirderChimp · Jan 15, 2017 at 02:45 AM

Hey, so i had a look at your code and its likely that the while loop in your CoolingOff co-routine is whats causing the problem.

I assume you dont want your CoolOff to complete in a single frame which is whats its currently doing. If you add the line. yield return null; below the laserWidth -= 0.01f this will make your co-routine yield for a frame and it will check the while loop again in the next frame and they - the 0.01 and so on until (laserWidth < minWidth && !laserOff).

Also i noticed that your cooldown(and other variables) is fps depended, this means that laserWidth -= 0.01f; will be called each frame and therefor doesnt take into account actual time.

So for example if you have 60fps your laserWidth will be -0.6 in 1 second. but for whatever reason if your fps is 30 it will take 2 seconds to get to -0.6. you should look into Time.deltatime to fix this problem.

Hope this helps ~Scott

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image emonegarand · Jan 15, 2017 at 04:53 AM 0
Share

Thanks, that did it, though I still experience a frame rate drop but its not too bad.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

298 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity freezes when I juggle Lists and SortedLists. 1 Answer

unity editor freezes on play, because of script 1 Answer

Function runs fine the first time but crashes if I run it twice? 1 Answer

Unity game freezes a few seconds after loading scene 2 Answers

How do I get my character to stop moving while he is attacking? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges