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 F3RILLA · Aug 26, 2014 at 01:21 PM · 2dtimerwaitforsecondscountdown

Countdown Timer or WaitForSeconds?

Hey Everyone,

I'm currently writing a script where I want to instantiate a game object randomly. I am planning to deploy the game on mobile and was wondering if I should use a countdown timer or WaitForSeconds instead?

If you were in my shoes, which one would you consider using and why would you use it?

Thanks, Beeraj

 private float Timer = 5.0f;
 
 if(Timer > 0)
 {
      Timer -= Time.deltaTime;
 }
 if(Timer <= 0)
 {
      Debug.Log("Execute Instantiate Script");
 }
Comment
Add comment · Show 4
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 Klarax · Aug 26, 2014 at 01:28 PM 0
Share

this is fine. but i would definatly recommend using Time.fixedDeltaTime incase frame rate fluctuates. could also put it in fixedUpdate func

avatar image robertbu · Aug 26, 2014 at 01:33 PM 1
Share

@$$anonymous$$larax - You would only want to use 'fixedDeltaTime' if you were executing this code in FixedUpdate(). Using 'fixedDeltaTime' in Update() would cause havoc in the ti$$anonymous$$g.

@$$anonymous$$larax - For a one-off spawn, consider Invoke(). For repeated, non-interrupted spawns, consider a Coroutine and WaitForSeconds(), or what you have here is fine.

avatar image Klarax · Aug 26, 2014 at 01:42 PM 0
Share

they are functions, which are slower tho init ? :P

avatar image F3RILLA · Aug 26, 2014 at 06:29 PM 0
Share

Thanks for the replies guys!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by senc01a · Aug 26, 2014 at 01:30 PM

I would use WaitForSeconds. This is mainly due to the feeling that the WaitForSeconds might be more optimized. I would imagine that WaitForSeconds would trigger some kind of internal event when the time is right, instead of calculating the Timer -= Time.deltaTime; on every update().

However keep in mind that this is simply due to an assumption of the underlying implementation of WaitForSeconds, and not based in any real evidence.

On the other hand, you might trust more your code, and think that manually keeping track of the amount of time is more reliable since you don't rely on somebody else's implementation.

Last but not least, since your time decrements happen on every tick, it is very well possible that Timer is not exactly 0, but a negative number by the time your IF statement is true. That would mean that you are skipping a few milliseconds. Perhaps the WaitForSeconds is more precise in that respect, although then again we don't really know because we don't have the implementation code.

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 F3RILLA · Aug 26, 2014 at 06:30 PM 0
Share

Thanks for the reply, that made things clearer!

avatar image
1

Answer by brycem24 · Aug 26, 2014 at 01:38 PM

I would instead use the InvokeRepeating() method. You could have it so your instantiate function randomly picks one. And use the Invoke Repeating to call the instantiate method at the intervals you want to generate the game objects. So in conclusion your script should look something like this. void Start() { InvokeRepeating("methodName", delay, intervalRate); }

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

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

25 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

Related Questions

Skipping over timer 1 Answer

How to start a timer after object is not active? 1 Answer

Photon Multiplayer Countdown TImer 2 Answers

disappear script that last a certain amount of time 2 Answers

Make a countdown timer say "go" once it reaches 0 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