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 voorash · Jan 21, 2017 at 06:51 AM · c#timerdeltatime

Is my problem delta time or just bad code

I have extensively researched this on here and elsewhere and while I have read a lot I am just not seeing where this code is not working. The problem only exists when the game is not the active tab in the browser. I read something in another post about some browsers throttling inactive tabs but I would think even it was only processing 1 frame a minute that deltaTime would be correct. public void earn() { if (count > 0) { time += Time.deltaTime; //if time goes over the time to act while (time >= timer) { //remove one cycle worth of time time -= timer; //give cash float cashToAdd = valueFormulaRemovedForClarity; gm.cash += cashToAdd; gm.totalEarnings += cashToAdd; } } }

Comment
Add comment · Show 6
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 Loui_Studios · Jan 21, 2017 at 04:01 PM 0
Share

Have you already tried using Time.fixedDeltaTime?

avatar image voorash · Jan 21, 2017 at 10:34 PM 0
Share

I haven't tried Time.fixedDeltaTime but it is my understanding that it doesn't really apply to my needs since I don't care how often this code runs but rather that the built up deltaTime is correct when it does. 1 FPS or 60 FPS are not relevant to me here.

avatar image James2Games · Jan 22, 2017 at 01:29 AM 0
Share

What problem is the code causing?

avatar image voorash · Jan 22, 2017 at 02:53 AM 0
Share

It seems like when it is running on a background tab the user is not getting cash at the correct rate. I guess I need to wire something up to see exactly what is going on.

avatar image James2Games voorash · Jan 22, 2017 at 03:15 AM 0
Share

The Time.deltaTime represents time in seconds it took to complete the last frame. So it seems like the game isn't running when the tab isn't active.

Have you enabled "Run in background" in the Player Settings?

avatar image voorash James2Games · Jan 22, 2017 at 03:40 AM 0
Share

I have. It appears to run fine when the browser is $$anonymous$$imized just not if it is active but on a background tab.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pengocat · Jan 21, 2017 at 03:24 PM

A while loop can easily stop the execution if used incorrectly. Everything outside the while loop is not updating while the while loop runs. So Time.deltaTime is only called once before the while loop so depending on whatever timer is it is becoming very brittle. You may want to restructure your code to use an if statement and a for loop instead.

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 voorash · Jan 21, 2017 at 10:30 PM 0
Share

Timer rarely changes but it will always be a value between .1 and 90.0 seconds

So if time equaled 5 $$anonymous$$utes and timer is 60 seconds it would execute the loop 5 times and be done. If a case occurs where it were to get stuck in that while loop things would have gone so horribly wrong that there would be no recovering from it gracefully anyway. The only case I can see that happening is if time = infinity which like I said means that something is hopelessly broken at that point anyway

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

10 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

Related Questions

Need help with making disappearing platforms. Error CS1525 1 Answer

Timer float not evaluating correctly when I check if it's less than another number 3 Answers

Timer only goes for .02 seconds 1 Answer

Traffic Light won't change colors State machine 0 Answers

is it possible to reset Time.time 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