Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by Bengooch7 · Jul 21, 2015 at 08:36 PM · javascripttimeprofilerframerate

Repeatedly creating infinite loops slows down code

So I posted this question earlier, but the mod closed it saying I should write my scripts in c# instead of JavaScript, which is NOT a solution.

Anyway,

So I've built a very simple prototype for a day/night system. The scene has a plane that is 8x8 as the ground, 3 little houses I made out of cubes, a tall skinny cube (just there to cast a shadow), and 2 short cubes to represent characters. I also have 2 text renderers set up, one shows the current time (to include seconds) and the other shows the date.

Then I have a handful of scripts that control flow of time and display of date/time.

I noticed that the longer my app runs in play mode, the lower the framerate drops. It starts around 200fps then over a matter of minutes hangs out around 2fps.

When I play with the profiler on, it seems to be the time.js script I have attached to one of the text renderers.

This is it's code:

 #pragma strict
 
  function Updatetime()
  {
      while(true)
      {
          var today = System.DateTime.Now;
          GetComponent.<TextMesh>().text = today.ToString("hh:mm" + " " + "tt");
          yield WaitForSeconds(0.5f); 
      }
  }
  
  function Start()
  {
      Updatetime();
  }
  
  function Update () {
     Updatetime();
 }


Now again, this was closed before anyone had the chance to offer advice on the topic. Is there a better way to write this? Or, does anyone know specifically what might be causing such a frame rate drop over a matter of minutes?

Thanks, community!

Comment
Add comment · Show 2
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 Hellium · Jul 21, 2015 at 08:36 PM 0
Share

About your problem. I think, the answer is quite obvious.

Each frame, you are calling the UpdateTime() function which has an infinite loop ! The longer your game runs, the more you have inifinite loops running.

Just call UpdateTime() in the Start function. Since UpdateTime is a coroutine inside an infinite loop, you don't have to call it each frame to "update" your function.

avatar image Bengooch7 · Jul 21, 2015 at 08:58 PM 0
Share

Hellium,

Thank you for explaining what the problem was, but best answer goes to rocket350 for explaining why it was a problem.

Thanks everyone!

2 Replies

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

Answer by rocket350 · Jul 21, 2015 at 08:38 PM

Ok, so the problem is that every frame, a script is launched which runs every 0.5 secondes. This means that every frame your computer has to proccess the loop in "Updatetime" again in addition to the ones lying back 0.5 seconds. My suggestion would be remove the "Updatetime()" from the "Update()" function.

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 Bengooch7 · Jul 21, 2015 at 08:54 PM 0
Share

Exactly what I needed. Thank you for breaking that down AND providing a possible solution.

Thank you!

avatar image
0

Answer by tanoshimi · Jul 21, 2015 at 08:37 PM

I believe what the mod in question said was "You're misunderstanding how to create a coroutine in unityscript. Carefully read the documentation on this topic and repost if you're truly stuck.".

Judging by the fact that you're posting again, I'm guessing that you're truly stuck, but perhaps you could share what you've tried since last you posted, and why, on every frame of Update(), you're launching another infinite-running function...?

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 Bengooch7 · Jul 21, 2015 at 08:56 PM 0
Share

That is, indeed, part of what the mod said, yes. But you didn't quote the section where the mod told me to use c# ins$$anonymous$$d of JavaScript because it is better. It would have been helpful for the mod to help me understand what was wrong with the script ins$$anonymous$$d of just telling me that I didn't understand how to write it properly.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Stuttering / performance issue with certain JS scripts 0 Answers

Very Bad Performance on Android 3 Answers

Screen go Gray and only Cursor active OnTriggerEnter 1 Answer

Using Time.DeltaTime doesn't result in framerate independence 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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