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 question was closed Dec 31, 2012 at 03:17 PM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Gumpert · Dec 31, 2012 at 12:30 PM · timewaitforsecondscounterdeltatimeseconds

How do I make a counter that goes up by 1 every second?

I am trying to make a Day and Night cycle in unity, and for that I thought I would need some kind of variable that goes up by one every second. I though this would be very simple to make, so I tried this:

var seconds : float = 0.0;

function Update() {

  seconds = seconds + (Time.deltaTime);    

}

However, something very strange happened - instead of the variable "seconds" going up by 1 every second, as soon as I pressed the play button at the top, the pause button next to it automatically went down with it, and then every time I tried to un-pause the game by pressing the button again, it played for 1 frame, and then paused again immediately afterwards. How has this bit of code manages to pause the game? Is this meant to happen, or have I broken unity somehow?

Comment
Add comment · Show 2
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 Gumpert · Dec 31, 2012 at 03:08 PM 0
Share

Sorry - don't worry about it - I found out how to do it!

avatar image Bunny83 · Dec 31, 2012 at 03:58 PM 0
Share

@Fattie: Right answer accepted? Wouldn't "Problem is not reproducible or outdated" fit better? Anyway... :D

1 Reply

  • Sort: 
avatar image
1

Answer by Golan2781 · Dec 31, 2012 at 12:39 PM

You can directly use the Time.time function to get the game time. You do not need to track it manually.

Generally, if you want something to happen every X seconds, you should look into the invokeRepeat and Coroutine functionality of Unity. InvokeRepeat is most appropriate for things always happening on the same timeframe, like for example toggling day states at fixed intervals. Coroutines are very good for making sequences ('do something every 5 seconds for five times total') or repeating actions with varying time intervals.
Using Update and checking states every frame is a lot more costly and can make cluttered code, though there are many things that need the level of control only Update offers.

On the issue of unity stopping: your code is fine. You should look for the fault at another point in your code.

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

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Add 1 Per Second to an Int 1 Answer

CountDown Timer Help (Seconds problem) 2 Answers

Movement using Time.deltaTime not working on Fast mac 3 Answers

Time.deltaTime making color lerp appear fast, but won't reach 1 1 Answer

Pause game that not using deltatime for movment 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