How to EASILY make a TIMER in Unity

Sdílet
Vložit
  • čas přidán 13. 06. 2024
  • Timers are often used in video games to give players a sense of urgency or display the current time on a special ability. In this session, we'll walk through how you can create a timer in Unity with c# for your own game that has a single click option to count up or count down. We'll also add a flash effect to the timer to its display once its time is up.
    ► Socials
    Twitter: / the_real_aia
    Discord: / discord
    My free web games: aiadev.fun/
    ► Get the script: unicornone.gumroad.com/l/time...
    ► Unity UI Tutorial: • Unity UI Tutorial | An...
    ► Microsoft doc on string.Format: docs.microsoft.com/en-us/dotn...
    ► Download Unity 3D: on.unity.com/3cbxhXY
    For more educational content on video game development, unity tutorials, and c# programming don't forget to subscribe!
    Chapters:
    0:00 Introduction
    0:30 Creating the unity timer script
    1:07 Basic unity countdown functionality
    8:45 Flashing the display once time has expired
    10:53 Adding an editor checkbox to switch between unity count down and count up
    12:31 Outro
    ········································­­·······································­·­····
    ****
    Get a 90% discount on a .fun domain for one year on www.get.fun using the coupon “AIA”
    ****
    ♪ Raspberry Music - A Lofi
    #unity3d #unitytutorial #gamedev

Komentáře • 42

  • @AIAdev
    @AIAdev  Před 2 lety +7

    How are you using a timer in your games?

    • @ivansav9142
      @ivansav9142 Před 2 lety

      for the onset of sudden death, approximately as in Worms

    • @jaymadethat1362
      @jaymadethat1362 Před 8 měsíci

      For a camera delay timer for main character.

  • @Likeableleaf
    @Likeableleaf Před 11 dny

    Amazing brother, you helped me while on stream! thank you so much for being so cool :)

  • @tontonton5791
    @tontonton5791 Před rokem +1

    hello, may I ask for help? my seconds timer seems to be invisible. but it functions fine. the minutes and the tens of seconds can be seen. only the seconds can't be seen going down. Thank you

  • @Juanzett
    @Juanzett Před 2 lety +1

    thanks for the nice tutorial!!! Great job

    • @AIAdev
      @AIAdev  Před 2 lety +1

      Thanks! Glad it was helpful:)

  • @skullydev5710
    @skullydev5710 Před rokem

    Thank you so much

  • @someguy262
    @someguy262 Před měsícem

    Been having a problem where, if countdown is enabled, that the time is never set and the timer constantly flashes when I start my game.

  • @Serious_Im
    @Serious_Im Před rokem

    Thanks!

  • @Cactusking1
    @Cactusking1 Před 8 měsíci

    Were can I get the same numbers as you have in the tutorial?

  • @raydani8748
    @raydani8748 Před rokem

    I didn't get any of the tabs come up when I dragged the script onto the canvas

  • @_arcadian._
    @_arcadian._ Před rokem

    Awesome tutorial, works great. One question thought : if we are working with a count up how do we make the current time of the timer customiseable? I want to use the timer as a clock, so i want my game to look like in the beginning its 06:00 and it ends at 12:00. Thanks in advance!

    • @_arcadian._
      @_arcadian._ Před rokem +1

      I figured it out, here is the solution for people who also want to do this:
      1. add a private float for the time you want the count up timer (stopwatch) to start. I called mine currentTime.
      2. the time formula is the same as the timeDuration (nf * 60f). I set mine to 6f, so that the timer would start at 06:00.
      3. In the ResetClock void, in the else statement, instead of making clock = 0, make it equal the float you added. In my case, clock = currentTime;
      Now, that should work by itself to make your stopwatch start a certain time, but if you want to make it stop at a certain time as well, here is how to do it:
      1. add a public bool for a time limit, and a public float a time limit. mine looked like this:
      [Header("Limit Settings")]
      public bool hasLimit;
      public float timeLimit;
      2. in void Update, add an additional if statement in the beginning. i renamed some of my variables because i am calling my script a clock, so instead of clock yours should say time/timer (however you have written it as) it is a bit complicated, so i will just paste it here:
      if (hasLimit && ((countDown && clock = timeLimit)))
      {
      clock = timeLimit;
      UpdateClockDisplay(clock);
      enabled = false;
      }
      3. under it, reference the Update Display void, i remained mine to UpdateClockDisplay(clock); but if you are following the tutorial yours is probably called UpdateTimer/TimeDisplay(time/timer) (i forgot which one is used and am kinda too lazy to check)
      4. in unity you should be able to set the time limit. in order for it to work in the current format, it should look something like this: 1200
      that's it! i managed to figure it out because i watched another tutorial by BMo, here is the link: czcams.com/video/u_n3NEi223E/video.html

  • @matthiuealmacen3815
    @matthiuealmacen3815 Před rokem

    Sir good am/pm, do you know how to make a pause for the countdown?

  • @IDENTICYEA
    @IDENTICYEA Před rokem

    hey man thanks

  • @rushabhgohel5182
    @rushabhgohel5182 Před 11 měsíci

    how can i display hours similer way you show minutes and seconds?

  • @kara_7z
    @kara_7z Před 2 lety

    TNX BRO

  • @terrenceskill3231
    @terrenceskill3231 Před rokem

    I need the Countdown for Rewarded Ads that only could be used once per hour

  • @yojan69
    @yojan69 Před rokem

    tysmmm

  • @ivansav9142
    @ivansav9142 Před 2 lety

    good shit.
    I just splurged on my first comment of all time on CZcams
    thanks a lot for a cool explanation

    • @AIAdev
      @AIAdev  Před 2 lety +1

      😮 maybe the coolest comment I’ve ever received TBH. You’re destined for greatness.

    • @ivansav9142
      @ivansav9142 Před 2 lety

      @@AIAdev is it possible to withdraw the received timer readings?

    • @ivansav9142
      @ivansav9142 Před 2 lety

      ok I took it from here -> string currentTime = string.Format("{00:00}{1:00}", minutes, seconds);

    • @AIAdev
      @AIAdev  Před 2 lety

      @@ivansav9142 im not sure what you mean

    • @AIAdev
      @AIAdev  Před 2 lety

      @@ivansav9142 did this solve your above question?

  • @Zap.zz.
    @Zap.zz. Před rokem

    how do save your best Times pls Respond Someone using PlayerPrefs

  • @TheGameLecturer
    @TheGameLecturer Před rokem

    I don't get it, mine flashes all the time.

  • @vc.2023
    @vc.2023 Před 10 měsíci

    How come you missed to mention digital font in your tutorial?

    • @AIAdev
      @AIAdev  Před 10 měsíci

      oh I didn't think it was important. It's from Dafont.com

  • @dkordy
    @dkordy Před 2 lety +1

    can you please help,NO ONE on the whole you tube dont tell how to stop cont down or count up when you need? can you help please!!!

    • @AIAdev
      @AIAdev  Před 2 lety +1

      you could add a new boolean variable like isPaused. Then in the update function do a check on isPaused. If it is true, return the function early. Then you just need to set the isPaused by whatever means you want

    • @dkordy
      @dkordy Před 2 lety +1

      @@AIAdev i dont understand but thanks for replay.

    • @dkordy
      @dkordy Před 2 lety +1

      I figured it out, thanks a lot! You're the only one who answered me. Thank you very much.

    • @cheery-hex
      @cheery-hex Před rokem

      Ikr right like what's the point of having a timer that won't stop when the level ends lmao

    • @Superspy1764
      @Superspy1764 Před rokem

      for anyone looking for a answer still all you do is make a public bool called isPaused and then at the top of the update void say
      if(isPaused)
      {
      return;
      }
      also to make sure it doesnt start paused add isPaused = false in the start void.
      so it would be void Start()
      {
      resetTimer();
      isPaused = false
      }
      then in void update
      {
      if(isPaused)
      {
      return;
      }

      if(countdown && timer > 0)
      {
      timer -= Time.deltaTime;
      updateTimerDisplay(timer);
      }

      else if(!countdown && timer < timerDuration)
      {
      timer += Time.deltaTime;
      updateTimerDisplay(timer);
      }

      else
      {
      flash();
      }
      }

  • @nearraindrop116
    @nearraindrop116 Před 8 měsíci

    Can someone either leave the code in the comments or help me out? I'm using unity 2D URP 2021.3.24f1 and i keep getting these 3 errors. I'm new to unity so if someone can help me out I would appreciate it!
    ERRORS:
    Assets\Timer.cs(31,38): error CS1002: ; expected
    Assets\Timer.cs(46,52): error CS1002: ; expected
    Assets\Timer.cs(47,52): error CS1002: ; expected
    Thanks in advance for any help!

    • @AIAdev
      @AIAdev  Před 8 měsíci

      looks like you're forgetting to end a statement, or multiple statements with semicolons. ;

    • @nearraindrop116
      @nearraindrop116 Před 8 měsíci

      @@AIAdev Ah thanks!

    • @nearraindrop116
      @nearraindrop116 Před 8 měsíci

      i forget to do that offten