Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

► Timers & Clocks ◄

Discussion in 'Assets and Asset Store' started by Stephen_O, Aug 8, 2015.

  1. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Hi, I'm glad you're enjoying.

    Feel free to send an email for an updated script, or you can add these functions to the script as an example

    Code (CSharp):
    1.  
    2. public void IncreaseTimerValue () {
    3.     gameTime += 5.0f;
    4. }
    or this if you want to specify the amount ( -5 would decrease the time value by 5 seconds )

    Code (CSharp):
    1.  
    2. public void IncreaseTimerValue (int value) {
    3.     gameTime += value;
    4. }
    You can also make line 38 in the script a public variable: gameTime . This will allow you to directly modify the timer value without a built in function.

    I'll spend some time tonight adding some built in functionality to modify the timer value and post an update to the store.
     
  2. MoribitoMT

    MoribitoMT

    Joined:
    Jun 1, 2013
    Posts:
    301
    Hi,

    I am interested in this product, I just want to ask couple of questions.

    The feature I am looking is; I have 5 days reward program, User claims the the first reward, and after 24 hours second reward is avaliable to be claimed, etc... I have couple of reward systems like these..

    Does your timer keep counting down when user exit the app, and come back again later ?
    Can I speed up the countdown ? ( Such as watch this video and skip 6 hours in countdown )
     
    Stephen_O likes this.
  3. hanso23

    hanso23

    Joined:
    Jul 22, 2012
    Posts:
    16
    Hey!

    Again sorry for my late reply & thanks for your solution! I've made the gameTime public, so I can access it via playmaker without a problem! Working perfectly!! \o/
     
    Stephen_O likes this.
  4. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    You're welcome, I also submitted an update to the asset to make that change permanent.
     
  5. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.1 submitted.

    • Added 3D Clock model for 3D clock prefab
    • Added Clock Sprite for uGUI clock prefab
    • Added AddTime (float value) method, use negative numbers to subtract or positive to add time.
    • Changed gameTime variable to public so it can be accessed directly if needed.
     
    Last edited: Jul 24, 2020
  6. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Hello,

    The timer will not count down when the app closes. The best way to handle this is to compare Timestamps as your use case requires tracking time outside of a running application.

    You would want to create a timestamp when an event happens (user claims reward) and save that value someplace with playerprefs or on a server. If your app is running you can then calculate the difference between the stored timestamp and the next available time using then use my asset to manage the timer while the application is running.

    I added a method to version 1.1 (currently pending review) to allow users to modify the current time value of a timer, with this method you can use negative numbers to subtract or positive to add time.
    Code (CSharp):
    1. AddTime (float value)
    I can look into adding an option/script to create and check a timestamp value using playerprefs to save the data, I've done this in the past in to manage UnityAds myself so it should not be too time consuming, however I don't know when it will be ready. I will post here with an update soon for this feature.
     
  7. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Timers & Clocks is 30% off for mega sale!
     
  8. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.1.2 submitted.

    This new version has a minor but useful change:

    • UGUI and 3d analog clock prefabs no longer require a Timer reference, their scripts now contain everything required to make the prefabs work without any configuration.
     
  9. Gatskop_Software

    Gatskop_Software

    Joined:
    Sep 21, 2014
    Posts:
    86
  10. george_00

    george_00

    Joined:
    Oct 23, 2015
    Posts:
    19
    Hello there,

    I have a quick question before I buy: I have a scene in my game where the player goes to a party, and I want a clock to display with the start time (say 8pm) when the scene begins, then I need this clock to run pretty fast, so after a short time it's 11pm and the party is over. Is it possible with this asset to set the start time in this way, and adjust the speed of the clock, so I can control how long the scene lasts?

    I can see in the video that you can call other events from the script - is it possible to set this so I could call them once the timer reaches a specified time, like 11pm?

    Thanks so much for your help.
     
  11. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Thanks for the question. The asset does not currently support that feature.. but I was able to add it without too much effort and it makes sense to add it. I'll submit the update soon; if you decide to buy and want the update sooner, you can email me an invoice number after you purchase and download the asset, then I'll forward you the update.

    With the update, you'll want to create a timer that has the following inspector configurations made:

    Timer State - Counting
    Timer Type - Count Up
    Set Start Time For Count Up - True
    Start Time - 72000 (72,000 into a day would be 8 pm)
    Finish Time - 82800 (82,800 into a day would be 11 pm)
    Timer Speed - increase based on how fast you want time to pass
    Times Up Event - assign your custom event to trigger at the finish time

    Then if you want to use a uGUI or 3D clock prefab, just add it to the scene and make the following inspector configurations:

    Use System Time - False
    Timer - assign the timer reference
     
    Last edited: Jul 24, 2020
  12. george_00

    george_00

    Joined:
    Oct 23, 2015
    Posts:
    19
    @Stephen_O Wow, THANK YOU! That's awesome! I really appreciate your help, I've just made the purchase so I'll send you the invoice number now :-D
     
    Stephen_O likes this.
  13. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.1.3 submitted.

    I had an additional request to add Text Mesh Pro support for UI Text output. I believe the TMPro namespace is available starting in 2018.2 for all projects, so this asset will now have an option to use either the default UI Text or Text Mesh Pro UGUI by selecting the new Text Type > Text Mesh Pro UGUI option in the inspector and assign a new TMP Text component reference.

    • Add option to set start time when a timer is counting up so clock prefabs can be able to read it as a time of day.
    • Option to use TextMeshPro text instead of default Unity text.
    • Version 1.1.3 requires Unity version 2018.2.1 or higher.
     
    Last edited: Jul 24, 2020
  14. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.1.4 submitted.

    • Improved inspector options layout.
     
    Last edited: Jul 24, 2020
  15. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.1.5 is now available

    • Added system time hour offset - allows simulation of timezones for different analog clock objects in the scene.
     
  16. InfiniteDesign8

    InfiniteDesign8

    Joined:
    Aug 22, 2017
    Posts:
    37
  17. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    There's no built in feature for time accumulation and saving that data for future use. The timer component is mostly for generic timed events, etc.

    I might take your inquiry and add a system like that in the future, it seems like it would be a good feature to have.
     
  18. chex35

    chex35

    Joined:
    Feb 4, 2019
    Posts:
    2
    Thank you for this! Is there a way to dynamically show milliseconds only after the minutes reaches 00?
     
  19. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    The timer script is basically formatting a float as it counts, you can make a reference to this value and check for when it's below 60.0f (less than a minute) or 1.0f (less than a second), then change the timer format options to only display the field you want.

    Add this example script to the object with the Timer component on it in your scene and it will only show milliseconds once the timer drops below 1 second or show milliseconds and seconds if the timer is less than 60 seconds.

    Example script:
    Code (CSharp):
    1.  
    2. using TurnTheGameOn.Timer;
    3. using UnityEngine;
    4.  
    5. public class FormatTimer : MonoBehaviour
    6. {
    7.     private Timer timer;
    8.  
    9.     void Awake()
    10.     {
    11.         timer = GetComponent<Timer>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if (timer.gameTime < 60.0f) // timer has less than 60 seconds - only display seconds and milliseconds
    17.         {
    18.             timer.timerFormat.milliseconds = true;
    19.             timer.timerFormat.seconds = true;
    20.             timer.timerFormat.minutes = false;
    21.             timer.timerFormat.hours = false;
    22.         }
    23.         else if (timer.gameTime < 1.0f) // timer has less than 1 second - only display milliseconds
    24.         {
    25.             timer.timerFormat.milliseconds = true;
    26.             timer.timerFormat.seconds = false;
    27.             timer.timerFormat.minutes = false;
    28.             timer.timerFormat.hours = false;
    29.         }
    30.     }
    31. }
    32.  
     
  20. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.1.7 submitted.

    • Changed gameTime from a float to double, this allows for 922337193600 max seconds allowed, which is 10,675,199 days. This is useful for saving a players game progression, with float values, the timer would previously break down due to floating point accuracy.
    • Added 'days' as a display option.
    • Fixed a bug where the save session Timer script would not always save/load the correct value.
    • Added PDF documentation.
     
    Last edited: Jul 24, 2020
  21. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.2 submitted.

    • Updated tutorial.
    • Updated documentation.
    • Increase max TimerSpeed from 100 to 10000.
    • Add 3D Analog Stopwatch prefab.
    • Add UI Analog Stopwatch prefab.
    • Changed Timer variable setStartTimeForCountUp name to setCountUpStartTime.
    • Moved Destroy Objects function to it's own class.
    • Moved Load Scene function to it's own class.
    • Fixed a bug where using a timer without a text component assigned would not properly output non-system time to analog prefabs.
    • Fixed a bug where some Timer properties were not properly serialized and did not support undo/redo or trigger a scene change.
     
    Last edited: Sep 22, 2020
  22. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.3 submitted



    • Timer script refactor.
    • Updated tutorial.
    • Updated documentation.
    • Added comments to Timer methods.
    • Added UI Digital Timer prefab.
    • Added 3D Digital Timer prefab.
    • Added tooltip descriptions to Timer script.
    • Removed Timer.setCountUpStartTime option, Timer.startTime is now always exposed.
    • Removed Timer prefab from Resources folder, it's now in the prefabs folder, renamed to 'UI Digital Timer'.
    • Removed Timer Format options, all formatting options are now controlled via 'Display Options'.
    • Moved Enums to their own script files.
     
  23. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.3 is now available.
     
  24. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.3.1 submitted.

    • Added 'Loop' option to Timer.
     
  25. awroosevelt

    awroosevelt

    Joined:
    Jul 13, 2018
    Posts:
    2
    Hi. so far I'm loving this. I do have a question though, I'm wanting to loop a timer to assist with a day/night cycle (torch on at night, then off in the morning). I'm able to get the timer itself to restart with a "times up event', but when it restarts, the events don't re-trigger. The clock seems to be working (verified with on screen UI text). Am I missing something, or just trying to do the impossible?

    PS. I see that you just added a "loop" function built right into the timer, but i don't see it. I just got the asset this morning.
     
  26. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Unity takes time to review the asset, since it's Friday, it may not be approved until Monday or later.

    If you like, send me an email with invoice number stephen@turnthegameon.com and I'll give you the updated asset package.
     
  27. awroosevelt

    awroosevelt

    Joined:
    Jul 13, 2018
    Posts:
    2
    Thanks for the updated script. Unfortunately, it didn't solve the issue I'm having. The timer does in fact loop, but after the first time though, it no longer triggers the events. I'm not sure if it's something that I'm doing wrong, or just a limitation of the script. Anyway, I though you might want to know. I still find TimerAndClocks very useful for other things, but without the events working on each loop, I'm not sure that it's useful for what I'm trying to achieve.
     
  28. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    It sounds like you're using the `Time Events`, I only tested the `Times Up Event` initially. I apologize for that, I just added a fix for this.

    It would be helpful to me if you have additional feedback to a request or support, please contact me in one place, so I only need to respond in one place (prefer here or email). We started on youtube, moved here, then email, now we're back here. I'll reply with a new package for you on email since you're requesting the package before approval as I can't post the package publicly via forum.
     
  29. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.3.1 resubmitted.

    • Added 'Loop' option to Timer.
    • Ensure 'TimeEvents' and 'TimesUpEvent' are both triggered when 'Loop' is enabled.
     
  30. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Version 1.3.1 is now available.
     
  31. ch1ky3n

    ch1ky3n

    Joined:
    Oct 26, 2017
    Posts:
    57
    hey there, I like to ask if this timer affected by the FPS?
     
  32. ch1ky3n

    ch1ky3n

    Joined:
    Oct 26, 2017
    Posts:
    57
    Or is it affected by Unity Time Scale?
     
  33. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Hello, the time calculation uses Time.deltaTime, it counts the time between the current and previous frame.
     
  34. profpaton

    profpaton

    Joined:
    Mar 24, 2021
    Posts:
    1
    I'm trying to use the timer to trigger various things like reduce a public integer variable. It would be a 24 hour countdown that would within the first 30 seconds of countdown reduce the public integer variable by one, reach the end and start over again reducing the variable again the same way and on and on. But I'm a bit new to Unity and your video tutorials about triggering events with your interface look completely different than on my screen. I see a configuration list that I do not completely understand without a good example. when I tell it to create 3 time events by setting "size" to 3, it gives me a bunch of things to fill in like "calls" "Element 0" "target" "method name" "mode" "object argument" "int argument", not like what is pictured in the latest video. Do you have any updated documentation or videos to help me out?
     
  35. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Hello, the latest video tutorial is accurate to the current asset version.



    At time 1:43 in the video I have an example that demonstrates how to use the 'Time Events' list.

    This is exactly what's shown in the video, the first thing I do is assign the Event Name, which changes 'Element 0' to 'Wave 1', then I assign the 'Event Time' value, followed by assigning the function GameObject.SetActive(false).
    TimerExample.gif

    Instead of calling GameObject.SetActive on the light, you would call a function in your own object/script that does what you need.
     
  36. CoachRivers

    CoachRivers

    Joined:
    Mar 16, 2020
    Posts:
    1
    Would the countdown timer be able to be set to a custom time by the user? and then started with the click of a button/reset with a click
     
  37. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    Yes, there are exposed functions and variables, so you can write a script that responds to the UI and passes it to the timer.

    https://timersandclocks.turnthegameon.com/api/timer
     
  38. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Hell ya man! I was one of the first to buy your clock asset years back. I was using it on a project I never made.
    I'm currently working on a project and i needed a timer and damn this thing does everything I needed it too.
    Even the leading zeros options. and and and?? TextMeshPRo seriously wow.

    Edit: I did find one thing I need but I can just code it. Figure id mention it anyways.
    Maybe make a option on the Timer where if there are no hours left then it removes the hours.
    so for 30 minutes even with Show hours enabled. It will show 30:00 instead of 00:30:00

    Edit: Actually just noticed another useful idea. Something I actually needed but I could probably work around it. I was hoping with hours disabled it would show 90 minutes for a hour and half instead of just merely cutting off the hours.
     
    Last edited: Jun 9, 2021
    Stephen_O likes this.
  39. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    I'm glad you're still finding use many years later :)
    Thanks for the feedback, if I find some time for new features I'll keep this in mind.
     
  40. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,647
    Thanks @Stephen_O

    Think i found a bug.
    When I call
    Code (csharp):
    1. CultureTimer.RestartTimer();
    It doesn't restart the timer like it says in the comments.
    Gonna change it to ResetTimer and then StartTimer instead to get around that.

    Edit: Another thing i just found. If you use timer.StopTimer();
    It will stop every timer instead of just the one im targeting.

    Turns out once you call StopTimer() you can never start any timer by script.
    Even if you call StartTImer()

    I think all this is related the same issue.

    Ok so it turns out the entire issue is using StopTimer. I found a work around by simply changing the TimerState to disabled instead of calling stop timer. That only works in my special situation though. Anyone needing to stop/pause a timer is gonna have a problem. My UI rebuilds its self on refresh from prefabs so this is why this works for me.

    Idk i didn't really test what happens if i try to set a disabled back to counting.
     
    Last edited: Jun 18, 2021
  41. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    When I test, it works as intended. RestartTimer calls ResetTimer and StartTimer:
    Code (CSharp):
    1. public void RestartTimer()
    2. {
    3.     ResetTimer();
    4.     UpdateUIText();
    5.     StartTimer();
    6. }
    Do you have setZeroTimeScale enabled in the time's up event? That would be the only reason one timer would impact another, timescale is global in unity and not local to specific timers. Each timer is its own monobehaviour, and does not share any data with another timer.
     
  42. Kunalz

    Kunalz

    Joined:
    May 11, 2018
    Posts:
    25
    Hey I'm planning on buying this asset but I want to know if it can be used for the purpose I am looking for which is ingame time/clock system.

    I need it for a rpg game where events etc will be played out based on ingame time, So I need total days, the ingame time (12 hr format, like 1:00am, 12:00pm etc) and day name like Monday Tuesday etc.

    I might make my own but if this can do something like that easily then It would save me some time
     
  43. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    The timer component can be used for time tracking tracking, but for your custom use-case, you would likely need a script to read the values and output to UI in the specific format you desire.
     
  44. hughsey1973

    hughsey1973

    Joined:
    Sep 7, 2021
    Posts:
    2
    Hello,

    I've just bought the Timer today and having a play with it.

    Could you tell me how I would go about stopping the timer when my player collides with another object.

    Thank You
     
  45. hughsey1973

    hughsey1973

    Joined:
    Sep 7, 2021
    Posts:
    2


    ***I've managed to achieve this. Thank you.
     
    Stephen_O likes this.
  46. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    I'm glad you got it working.
     
  47. Thall33

    Thall33

    Joined:
    Sep 18, 2013
    Posts:
    134
    I would love to be able to input my own 3D meshes for the timers and countdowns.
     
  48. EmpireStudios

    EmpireStudios

    Joined:
    Oct 23, 2018
    Posts:
    24
    Does this work for mobile?
     
  49. PinblendStudios

    PinblendStudios

    Joined:
    Sep 19, 2020
    Posts:
    13
    Hello, i sent over an email, but i am trying to figure out how to cause the timer to pause if the player pauses the game... I am very new to coding etc etc-- any help would be appreciated.
     
  50. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    You can set the game's timescale to 0 when you pause it, this will stop the timer and anything else that uses time.deltaTime - then return it to 1 when you unpause it will allow the time to pass.
    Code (CSharp):
    1. Time.timeScale = 0;
    2. Time.timeScale = 1;
    You can also call the Start/Stop functions in the timer script.
    Code (CSharp):
    1. Timer.StopTimer()
    2. Timer.Startimer()
    Or you can toggle the timer state in the timer script.
    Code (CSharp):
    1. Timer.timerState = TimerState.Disabled;
    2. Timer.timerState = TimerState.Counting;