Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 tapir · Oct 21, 2012 at 01:37 AM · mobileoptimizationtimeyieldwaitforseconds

Waitforseconds vs Time

Ok so my first question.

Since Im developing for mobile I want to optimize as much as posible.

I got two different ways of implementing a solution and wanted to know what is better.

The problem is this, I have Function 1, Function 2 .... Function n, each function its called each "n" seconds.

I wanted to know which solution is best A (with yield WaitForSeconds) or B (using Time)

Code A

 private var waitFunction01 = false;
 private var waitFunction02 = false;
 
 function Update () 
 {
     if(!waitFunction01)
         Function01();
         
     if(!waitFunction02)
         Function02();
 }
 
 function Function01()
 {
     waitFunction01 = true;
     
     Debug.Log("Function01 is running");
     
     //other code
     
     yield WaitForSeconds(3.0);
     
     waitFunction01 = false;
 }
 
 function Function02()
 {
     waitFunction02 = true;
     
     Debug.Log("Function02 is running");
     
     //other code
     
     yield WaitForSeconds(2.0);
     
     waitFunction02 = false;
 }

Code B

 private var startFunction01 = 0.0;
 private var startFunction02 = 0.0;
 
 function Update () 
 {
     if(Time.time - startFunction01 > 3.0)
         Function01();
         
     if(Time.time - startFunction02 > 2.0)
         Function02();
 }
 
 function Function01()
 {
     Debug.Log("Function01 is running");
     
     //other code
     
     startFunction01 = Time.time;
 
 }
 
 function Function02()
 {
     Debug.Log("Function02 is running");
     
     //other code
     
     startFunction02 = Time.time;
     
 }


I find Code A to be "cleaner" and more precise, but I really want to hear what you guys think.

Thanks, Gustavo

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Noah-1 · Oct 21, 2012 at 02:51 AM

I would use Time, as stated in UnityGems, coroutines are complicated overkill if you just want something to happen in the future, check it out, it might help:

UnityGems---> Making something happen in the future

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 tapir · Oct 21, 2012 at 03:19 AM 0
Share

thanks men that link is really useful, I had no idea about the invoke function.

avatar image Noah-1 · Oct 21, 2012 at 03:24 AM 0
Share

glad to help :)

avatar image
0

Answer by Uniity3D · Oct 21, 2012 at 04:52 AM

Code "A" looks cleaner as you stated, but coroutines are often FPS dependant WaitForSeconds is limited by the framerate, as they're checked once per frame loop so I would go with code B.

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 Eric5h5 · Oct 21, 2012 at 04:54 AM 0
Share

Update is, by definition, only checked once per frame, so there is no difference in accuracy.

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

12 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

Related Questions

Temporarily disable blayer movement? 1 Answer

What is the most CPU efficient and smooth way to create timed events? 2 Answers

How to delay a function (how to use WaitForSeconds() and yield) 1 Answer

Boost Script Time problems 1 Answer

Yielding for a changing amount of time? 2 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