• 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 /
avatar image
0
Question by Wideopen411 · Aug 18, 2015 at 12:53 AM · rigidbodycoroutinelag

Lag when activating a Coroutine

Hey everyone, I am having some problems with lag whenever I activate a script. I have a jackhammer in game, then I have a GUI button firing off an "autojack" script. Everything works as expected. However, there is about a three second lag before the jackhammer starts jacking. Its not a lag in the GUI system. I have tested this by manually activating the script in the inspector during gameplay, and there is still a 3 second lag. So I know there is something wrong with my code. Am I even going about this the right way? Its a rigidbody, so I am putting it in fixedupdate. But I dont know if I'm actually doing this right. I'm new to c#, so any advice would be awesome! Thanks

 using UnityEngine;
 using System.Collections;
 
 public class AutoJack : MonoBehaviour 
 {
     public float thrust;
     public Rigidbody rb;
     void FixedUpdate()
     {
         StartCoroutine(MyCoroutine());
     }
 
     IEnumerator MyCoroutine()
     {
         yield return new WaitForSeconds(.1f);
         JackDown();
         yield return new WaitForSeconds(1);
         JackUp();
         yield return null;
     }
 
     void JackUp ()
     {
         rb.AddForce(Vector3.up * thrust);
     }
 
     void JackDown ()
     {
         rb.AddForce(-Vector3.up * thrust);
     }
 }
 

Comment
Add comment · Show 1
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 $$anonymous$$ · Aug 18, 2015 at 12:58 AM 0
Share

It looks like you might be starting the CoRoutine many times (once for each FixedUpdate()). $$anonymous$$ake sure that you are only starting a single one.

2 Replies

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

Answer by Priyanshu · Aug 18, 2015 at 04:33 AM

Since you start Coroutine in FixedUpdate. There will be hundreds of corutines running within seconds of the script activation. Thus eating your processing power. Try this instead.

  void Start()
      {
          StartCoroutine(MyCoroutine());
      }
  
      IEnumerator MyCoroutine()
      {
          yield return new WaitForSeconds(.1f);
          JackDown();
          yield return new WaitForSeconds(1);
          JackUp();
          yield return  new WaitForSeconds(timeWhichYouWantToGiveHammerToGoUP);
          StartCoroutine(MyCoroutine());
      }

This code will run only one coroutine at a time continuously.

Comment
Add comment · Show 6 · 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 Wideopen411 · Aug 18, 2015 at 06:30 AM 0
Share

That is perfect thanks!! I never thought of restarting a Coroutine inside of the coroutine itself! That gets rid of the update problem altogether, and it runs so much more precisely. I do have one problem with it though. Whenever the script is set to false, to coroutine still runs continuously because it never exited it before the script was set to false.

On my actual "autojack" activation button I have this script.

 private IEnumerator PopButton ()
     {
         player.GetComponent<AutoJack>().enabled = true;
         yield return new WaitForSeconds(5);
         player.GetComponent<AutoJack>().enabled = false;
 }
 

So is there a way I can call the coroutine on the autojack script and tell it to stop before setting ().enabled = false?

avatar image daniel-eherbert · Aug 18, 2015 at 06:42 AM 1
Share

You could alter the Coroutine to check for enabled before restarting itself:

 if (enabled) {
     StartCoroutine($$anonymous$$yCoroutine());
 }
avatar image daniel-eherbert · Aug 18, 2015 at 06:45 AM 1
Share

Additionally there is StopCoroutine(...) if you cache the coroutine in a variable.

 private IEnumerator PopButtonCoroutine;
 
 ....
 
 //start the coroutine
 PopButtonCoroutine = PopButton();
 StartCoroutine(PopButtonCoroutine);
 
 ....
 
 //stop the coroutine
 StopCoroutine(PopButtonCoroutine);
avatar image Wideopen411 · Aug 18, 2015 at 06:46 AM 0
Share

I think I figured it out in AutoJack I put public bool routine;

     IEnumerator $$anonymous$$yCoroutine()
     {
         yield return new WaitForSeconds(.1f);
         JackDown();
         yield return new WaitForSeconds(.1f);
         JackUp();
         yield return  new WaitForSeconds(.001f);
         if (routine != true) 
         {
             StopCoroutine($$anonymous$$yCoroutine());
         }
         StartCoroutine($$anonymous$$yCoroutine());
 //        yield return null;
     }

then in the button script i put

 private IEnumerator PopButton ()
     {
         player.GetComponent<AutoJack>().enabled = true;
         yield return new WaitForSeconds(5);
         autoJack.routine = false;
         player.GetComponent<AutoJack>().enabled = false;
 
     }

Does this seem like the right approach? Im still a complete beginner at code. So let me know if there is a better way. Thanks again!

avatar image Wideopen411 · Aug 18, 2015 at 06:58 AM 0
Share

Thanks daniel for the reply, I'll try that! I didnt refresh this page before adding my next comment, so I didnt get to see your reply right away.

Show more comments
avatar image
0

Answer by Sethhalocat · Aug 18, 2015 at 01:03 AM

I dont mean to be so dull, but you might need a better computer, If you are spawning in the same thing multiple times and it laggs that means you need more ram or a faster computer. You might need to make the thing your spawning in a bid less detailed, if its particles you need less particles, if its a model, give the model less proxies...

Comment
Add comment · Show 4 · 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 Wideopen411 · Aug 18, 2015 at 01:15 AM 0
Share

I thought about that too. I dug completely through the profiler, and this script doesn't even really have any affect. On this rig i have an i7-4820k with 128mb of ram, and a 780ti. No matter what rig I build it for, it still has this little 3 second lag after I enable the script.

There is nothing being instantiated. Im just setting this script to enabled. This script is attached to the player along with a character controller. So you control the character as normal, then whenever you activate this script, it makes them bounce up and down like crazy. I have this script running for 5 seconds, then I do a .enabled = false for this script. Then if they hit the button again, it just reactivates it for another five seconds.

avatar image Eno-Khaon · Aug 18, 2015 at 01:23 AM 0
Share

I really, really hope you don't actually have 128mb of RA$$anonymous$$, because even Windows XP needed 512mb to run adequately and Operating System requirements have continued to rise every year.

If you have 128mb of RA$$anonymous$$, I would honestly be impressed if anything done in Unity takes less than 5-10 $$anonymous$$utes due to the need to write into the page file, then swap that back into RA$$anonymous$$, then trade that back for something else over and over again.

avatar image Wideopen411 · Aug 18, 2015 at 02:58 AM 0
Share

I meant 128 gigs, 8 16gb sticks. obviously I'm not going to have a k series i7 with a 780, and put 128mb in it. I built this rig for photogrammetry, so it was useful then. I know its not needed or even used in most programs, but I know for a fact this has nothing to do with my computer speed. The script doesn't fire until three seconds after its activated. Should I be adding the trusts inside of the coroutine itself ins$$anonymous$$d of sending it to the other functions that handle it? Or am I just approaching this problem wrong?

avatar image $$anonymous$$ · Aug 18, 2015 at 03:00 AM 0
Share

It's hard to really diagnose without seeing the project. $$anonymous$$aybe try a debug statement ins$$anonymous$$d of adding a force just in case the force being added isn't large enough to be immediately noticeable.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Saving data question 1 Answer

Download an audio file in background without lags. 0 Answers

Multiplayer Rigidbody Vibration 2 Answers

Why does this simple follow script make my game lag? 2 Answers

Does collider size affect performance? 1 Answer


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