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 /
avatar image
0
Question by Ego65 · Sep 26, 2016 at 10:31 AM · fpscoroutinewaitforsecondsperformance optimizationupdate function

waitforseconds in update good for perfomance ?

hey at all,
i'm experimenting with waitforseconds in update(). In the manual is explained that this would greatly reduce the number of checks carried out.
So i changed my Script from :
void Update () {

                 thisMiss1conf = Miss1confirmedScr.Miss1confirm;
         
                 if(thisMiss1conf)
                 {
                         Einl_Sound_2.SetActive(true);
                         thisRatPack.SetActive(true);
                         Miss1Canvas.SetActive(false);
                         BeendenButt.SetActive(false);
                 }
 
                 if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf &&
                     !jalthiPack1[4].activeSelf &&thisMiss1conf &&  thisRatPack.activeSelf)
                 {
                         Miss1done = true;
                         thisMissaccImg.SetActive(true);
                 }
         }

to :

         void Start () 
         {
                 Miss1confirmedScr = Einl1Obj.GetComponent<Einleitung_1>();
                 thisRatPack.SetActive(false);
                 Einl_Sound_1.SetActive(true);
                 Einl_Sound_2.SetActive(false);
                 StartCoroutine(xxx());
         }
     
         void Update () 
         {
                 
                 thisMiss1conf = Miss1confirmedScr.Miss1confirm;
                 StartCoroutine("xxx");
                 if(thisMiss1conf)
                 {
                         Einl_Sound_2.SetActive(true);
                         thisRatPack.SetActive(true);
                         Miss1Canvas.SetActive(false);
                         BeendenButt.SetActive(false);
                         StartCoroutine("xxx");
                 }
 
                 if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf &&
                     !jalthiPack1[4].activeSelf &&thisMiss1conf &&  thisRatPack.activeSelf)
                 {
                         Miss1done = true;
                         thisMissaccImg.SetActive(true);
                         StartCoroutine("xxx");
                 }
                 StartCoroutine("xxx");
         }
 
 
         IEnumerator xxx()
         {
                 yield  return new WaitForSeconds(0.1f);
         }
 

well, it works and i dont get any errors. What i would like to know if its really correct or nonsens. And if its really a benefit for perfomance although i cant "see" a big difference in fps.
But i think "if so" i have several more scripts in which the functions in Update() hasn't to be calculated absolutely precise in every frame and could be also changed in this way.
And in summary it would make a difference for perfomance, wouldn't it?
I'm very nosily about your opinion or tips and tricks

yours

Comment
Add comment · Show 3
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 pako · Sep 26, 2016 at 10:50 AM 1
Share

This is not correct usage. You are starting the coroutine xxx at least twice every single frame.

Could you provide a link to the place in the documentation you refer?

avatar image pako · Sep 26, 2016 at 10:55 AM 0
Share

BTW, if you refer to the DoCheck() coroutine in:

https://docs.unity3d.com/$$anonymous$$anual/Coroutines.html

It's being used in a different way. The coroutine is supposed to get started only once (not every single frame) , in order to call the ProximityCheck() method according to the WaitForSeconds interval, rather than every single frame.

avatar image Ego65 pako · Sep 26, 2016 at 11:44 AM 0
Share

thx for the fast reply!
yes, thats the right link. I thought i could do it similar to the "Fade()" example. In which Fade() is in the update there. So is $$anonymous$$e completely false ? or would it be ok to set my coroutine only once at the end of the update() ?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by pako · Sep 26, 2016 at 12:30 PM

Fade() is started inside Update() but ONLY if the "f" key is pressed. So, if the end user pressed the "f" key repeatedly, it would start a new instance of the Fade() coroutine per key press. Not a good practice!

Similarly, if you start your coroutine at the end of Update, a new instance of the coroutine would get started every single frame, very bad for performance, and it's not intended behavior anyway.

However, you could have an if condition inside Update() to start the coroutine, e.g.

 if(gameOver)
 {
     StartCoroutine("xxx");
 }
 

This would work, because in practice you can't have gameOver = true occurring very frequently.

Instead of gameOver you can use any condition you like of course, but you should have in mind that the frequency that this boolean variable gets set to true is small.

You must also put the behavior that needs repeating every 0.1s inside the xxx coroutine, not inside Update. This is illustrated in both Fade() and DoCheck() in the examples.

In other words, if you just start the xxx coroutine, its WaitForSeconds(0.1f) will not affect at all in practice the rate at which Update() is called. To be more precise, the rate at which Update() gets called might be affected by other things in your code, but not by the WaitForSeconds(0.1f) itself.

Comment
Add comment · 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
0

Answer by Ego65 · Sep 26, 2016 at 03:33 PM

puuh pretty confusing for a beginner ):

if i understand you wright the code should look like this (?) :

 public bool nevertrue = true; // never become false
 
 void Update () 
         {
                 if(nevertrue)
                 {
                         StartCoroutine("xxx");
                 }
         }
 
 
         IEnumerator xxx()
         {
                 thisMiss1conf = Miss1confirmedScr.Miss1confirm;
                 if(thisMiss1conf)
                 {
                         Einl_Sound_2.SetActive(true);
                         thisRatPack.SetActive(true);
                         Miss1Canvas.SetActive(false);
                         BeendenButt.SetActive(false);
                 }
 
                 if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf  
                                 && !jalthiPack1[4].activeSelf &&thisMiss1conf  &&  thisRatPack.activeSelf)
 
                 {
                         Miss1done = true;
                         thisMissaccImg.SetActive(true);
                 }
                 
                 yield  return new WaitForSeconds(0.1f);
        }  

i've tried that and all seems to work i'm just hoping that it will take effect even if its not noticeable.

Comment
Add comment · Show 2 · 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 pako · Sep 26, 2016 at 04:31 PM 1
Share

I said that gameOver must NOT be true VERY FREQUENTLY, not never. So, if you should have a "sometimesTrue" variable ins$$anonymous$$d of nevertrue. Otherwise, it will never start the coroutine.

Also, the code inside xxx doesn't repeat at all, so you don't gain anything like this! However, let's introduce another variable "isRunning", which is true while the game is running. Then xxx becomes:

          IEnumerator xxx()
          {
              While(isRunning)
              {
                  this$$anonymous$$iss1conf = $$anonymous$$iss1confirmedScr.$$anonymous$$iss1confirm;
                  if(this$$anonymous$$iss1conf)
                  {
                          Einl_Sound_2.SetActive(true);
                          thisRatPack.SetActive(true);
                          $$anonymous$$iss1Canvas.SetActive(false);
                          BeendenButt.SetActive(false);
                  }
  
                  if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf && !jalthiPack1[3].activeSelf  
                                  && !jalthiPack1[4].activeSelf &&this$$anonymous$$iss1conf  &&  thisRatPack.activeSelf)
  
                  {
                          $$anonymous$$iss1done = true;
                          this$$anonymous$$issaccImg.SetActive(true);
                  }
                  
                  yield  return new WaitForSeconds(0.1f);
              }
                  
         }


Also, in this case, you don't need to start the coroutine inside Update() but only once. This can happen maybe inside Start(), or your own method StartGame().

e.g.

     StartGame() //call to start
     {
          isRunning = true;
          StartCoroutine("xxx");
     }

     EndGame() //to end the while loop in the coroutine
     {
          isRunning = false;
     }

I hope this is more clear now.

avatar image Ego65 pako · Sep 27, 2016 at 05:17 AM 0
Share

oh oh me stupid :)
I've done it your way. Ins$$anonymous$$d an EndGame() i've added:
if($$anonymous$$iss1done) { isRunning = false; } yield return new WaitForSeconds(0.1f);
All seems to work and isRunning is set to false correctly. This way the Update() isn't necessary anymore !

many many thx for your help and patience !

just to make sure that my script is ok :

         void Start () 
         {
                 $$anonymous$$iss1confirmedScr = Einl1Obj.GetComponent<Einleitung_1>();
                 thisRatPack.SetActive(false);
                 Einl_Sound_1.SetActive(true);
                 Einl_Sound_2.SetActive(false);
 
                 isRunning = true;
                 StartCoroutine(xxx());
         }
 
         IEnumerator xxx()
         {
                 while(isRunning)
                 {
                         this$$anonymous$$iss1conf = $$anonymous$$iss1confirmedScr.$$anonymous$$iss1confirm;
                         if(this$$anonymous$$iss1conf)
                         {
                                 Einl_Sound_2.SetActive(true);
                                 thisRatPack.SetActive(true);
                                 $$anonymous$$iss1Canvas.SetActive(false);
                                 BeendenButt.SetActive(false);
                         }
 
                         if(!jalthiPack1[0].activeSelf && !jalthiPack1[1].activeSelf && !jalthiPack1[2].activeSelf 
                         && !jalthiPack1[3].activeSelf  && !jalthiPack1[4].activeSelf &&this$$anonymous$$iss1conf  &&  thisRatPack.activeSelf)
 
                         {
                                 $$anonymous$$iss1done = true;
                                 this$$anonymous$$issaccImg.SetActive(true);
                         }
                         if($$anonymous$$iss1done)
                         {
                                 isRunning = false;
                         }
                         yield  return new WaitForSeconds(0.1f);        
                 }
         }

 


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

65 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

Related Questions

fps calculation with while loop in coroutine 2 Answers

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

Coroutines WaitForSeconds – uneven spacing 2 Answers

How to make WaitForSeconds work as a repeating/looping timer? 1 Answer

Script gets stuck on WaitForSeconds() 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