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 FallingSkies17 · Sep 16, 2016 at 10:28 PM · coroutinetime.deltatimeefficiencyincrement

Should I use a Coroutine function or a Time.deltaTime equation to add to values overtime?

I'm trying to make an incremental game of sorts. As you may know, incrementing numbers is a huge part of this task and should be done as efficiently as possible. I've come across two seemingly similar ways to do this that both seem viable and popular. The first way is simply using a Coroutine and having it add a certain value after a yield return new WaitForSeconds(waitTime) command. The second way is to just constantly add values using something like value += amount * Time.deltaTime and putting it in the Update function. I'd like to know which would be less resource intensive since I'll be adding tens to hundreds of these, or possibly a new way that is more efficient than both of my known methods. Any information on this topic would be great, as I have not found much in my search queries. Edit: I thought I would also mention that I am programming in C# and would prefer any code given in C#, although I could transpose if need be.

Edit 2: I also realize you could call an InvokeRepeating() call and simply pass in a function that adds a certain value at a set interval. I am still unsure of how any of these three methods compare in efficiency. Currently, it would seem that the Time.deltaTime equation would be the least resource intensive since it is just a simple equation rather than a whole function, but I am not too sure about that reasoning. At this moment in time, I will likely implement my incrementing functions using that equation and see how it goes. I will be using the general equation of:

 value += (valueToAddEachInterval * (1 / timeInterval)) * Time.deltaTime;

This will allow me to increment a value by a certain amount in a given interval, such as 5 every 12 seconds. I am still eager to hear back from other users and the techniques they use for such implementations.

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
0
Best Answer

Answer by jdean300 · Sep 17, 2016 at 12:15 AM

Doing this inside of update will likely have better performance than using coroutines - however, the difference will be extremely small. Even doing this hundreds of times concurrently is trivial. You won't notice the difference until you're doing it on the order of a few thousand times simultaneously, if at all.

I am pretty confident that this will not be the performance bottleneck of your game. Write whatever code is easiest for you to understand and when you start having performance problems profile your code to determine where you should optimize. This smells like premature optimization which is commonly referred to as "the root of all evil".

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 FallingSkies17 · Sep 17, 2016 at 12:20 AM 0
Share

Thank you for your answer. I had heard a few rumors that multiple coroutines could drastically decrease performance, although, I wasn't sure how reliable that information was. I was mostly just curious if there was a different more optimal or easier way to implement this functionality. If you say it is $$anonymous$$uscule in performance then it seems I have nothing to worry about.

avatar image jdean300 FallingSkies17 · Sep 17, 2016 at 12:23 AM 0
Share

I haven't had an issue running many coroutines at once - what may be an issue (I have no idea, just speculating) is if you have many coroutines executing on one object.

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

73 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

Related Questions

Time.deltaTime and difference of Time.time not matching 0 Answers

Improving Click-to-Move Movement Script (2D) 0 Answers

Unity coroutine movement over time is not consistent/accurate? 1 Answer

Getting variable from script while keeping other code from it 1 Answer

How to deactivate a dropdown list when changing UI tabs 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