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 Bensas · Aug 14, 2014 at 01:50 PM · gameperformanceeventstiming

What's the most efficient way to time events?

Up until now, whenever I needed to time something like an animation or the rate at which enemies spawn in a game I would use an integer variable to which I would add 1 on the update function. When that number reached a certain amount (say 120 if I wanted a 2 seconds delay), It would activate whatever needed to be activated.

It does, however, feel like a really primitive method, and I'm wondering what is usually used for timing events. Also, would changing the method affect performance?

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

Answer by christoph_r · Aug 14, 2014 at 02:07 PM

Yes, it's usually a good idea to do as little as possible in Update, usually only checking for user input. There are generally two ways to time code: Coroutines and using Invoke. Especially if you're new, coroutines can take some effort to get into, Invoke is usually the easier method. Of course both of those are much more performance-friendly than changing a value in Update, even though if it's not thousands of scripts doing that, it shouldn't be a big issue either. What can be an issue, though, is that not all frames last the same amount of time, so if you ever need to use that method again, make sure to use FixedUpdate. Again, though, coroutines and Invoke are far superior.

Comment
Add comment · 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
1

Answer by MrSteve1 · Aug 14, 2014 at 02:05 PM

essentially the opposite of your current way to be honest. Typically my countdown timers tend to look like this.

 var timer : float = 20;
 
 function Update()
 {
 timer -= Time.deltaTime;
 if(timer < 0)
 {
 doSomeCoolStuff();
 timer = 20;
 }
 }
 
 function doSomeCoolStuff()
 {
 print("Hello World");
 }

and then I would evolve from there.

Hope this helps!

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 CHPedersen · Aug 14, 2014 at 02:31 PM 1
Share

This is fine for repeating events that must continue to occur as long as the script is enabled. But checking a counter value in Update for one-off events is inelegant because Update will continue to check the variable over and over again even though it reached 0 a long time ago.

The coroutine is the established way to trigger timed events, and are an often-used code mechanic in Unity3D. You're better off learning them sooner rather than later, and applying them for this purpose. It will give you cleaner, more efficient code.

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

23 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

Related Questions

A node in a childnode? 1 Answer

Plugin Container for Firefox/Unity Web Player crash whenever I try to load Dead Trigger 2 0 Answers

you leave a question by pressing a button? 3 Answers

Most performant way to schedule a callback at a time, in the next frame and be able to insert extras once scheduled. 1 Answer

Unity Builden Game Crashes 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