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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by lexen1 · Mar 22, 2014 at 02:06 AM · c#timerreference

is it cheaper to run timer another, or refer to other timer?

Hey everyone. Thanks for reading my question. Have multiple scrips running timers that could all line up and my question is: Would it be faster to have them all run their own timer, or refer to another scripts timer once a frame.

Something like this:

 public int framesBetweenCheck = 50;
 private int framesBetweenCheckTemp = 0;
 
 private void FixedUpdate () {
         framesBetweenCheckTemp++;
         if (framesBetweenCheckTemp >= framesBetweenCheck) {
            // Do something;
         }
     framesBetweenCheckTemp = 0;
     }

or something like this:

 private int framesBetweenCheckTemp = 0;
 public GameObject _timer;
 private TimerScript _timerScript;
 
 
 private void Start () {
         _timer = GameObject.Find ("Timer");
         _timerScript = _timer.GetComponent<TimerScript> ();
 }
 
 private void FixedUpdate () {
         if (_timerScript.temp < 1) {
                 // Do something
         }
 }

I could be running this timer on a lot of scripts, so the cost or benifit would be amplified. Thanks again for reading my question!

Comment
Add comment · Show 1
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 Benproductions1 · Mar 22, 2014 at 07:23 AM 0
Share

Just a note on timers, generally you want to compare real time values, rather than the number of time-steps. This has both the advantage of saving a read/write operation of a variable and has a much larger accuracy, since the time between FixedUpdate calls is never guaranteed.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Mar 22, 2014 at 02:53 AM

I think it's up to what's your need from the timer script, because if you use a single component to manage this then you know that the counter is shared across all the objects that is checking for it.

If you're fine with having a shared counter, then I'd suggest something like the second (a TimerScript) but using a delegate callback instead of check the value in each FixedUpdate cycle, for example:

TimerScript:

 using UnityEngine;
 using System.Collections;
 
 public class TimerScript : MonoBehaviour {
 
     public System.Action callback;
     public int framesBetweenCheck = 50;
     private int framesBetweenCheckTemp = 0;
 
     void FixedUpdate () {
         framesBetweenCheckTemp++;
         if (framesBetweenCheckTemp >= framesBetweenCheck) {
             // Do callback
             if (callback != null)
                 callback();
             framesBetweenCheckTemp = 0;
         }
     }
 }
 

Your other script:

 using UnityEngine;
 using System.Collections;
 
 public class Test : MonoBehaviour {
 
     void Start () {
         GameObject _timer = GameObject.Find ("Timer");
         TimerScript timerScript = _timer.GetComponent<TimerScript> ();
         timerScript.callback += OnTimerElapsed;
     }
     
     void OnTimerElapsed () {
         // Do something
         Debug.Log("OnTimerElapsed >> " + name);
     }
 }
 
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 lexen1 · Apr 04, 2014 at 04:31 AM 0
Share

Thanks man! I don't really understand what a callback is though. What is the difference between a callback and just a traditional check?

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

22 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

Related Questions

timer problem 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# countdown timer 9 Answers

Problem calculating time taken to complete level 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