How to EASILY make a TIMER in Unity
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
How are you using a timer in your games?
for the onset of sudden death, approximately as in Worms
For a camera delay timer for main character.
Amazing brother, you helped me while on stream! thank you so much for being so cool :)
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
thanks for the nice tutorial!!! Great job
Thanks! Glad it was helpful:)
Thank you so much
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.
Thanks!
Were can I get the same numbers as you have in the tutorial?
I didn't get any of the tabs come up when I dragged the script onto the canvas
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!
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
Sir good am/pm, do you know how to make a pause for the countdown?
hey man thanks
how can i display hours similer way you show minutes and seconds?
TNX BRO
❤
You're welcome!
I need the Countdown for Rewarded Ads that only could be used once per hour
tysmmm
good shit.
I just splurged on my first comment of all time on CZcams
thanks a lot for a cool explanation
😮 maybe the coolest comment I’ve ever received TBH. You’re destined for greatness.
@@AIAdev is it possible to withdraw the received timer readings?
ok I took it from here -> string currentTime = string.Format("{00:00}{1:00}", minutes, seconds);
@@ivansav9142 im not sure what you mean
@@ivansav9142 did this solve your above question?
how do save your best Times pls Respond Someone using PlayerPrefs
I don't get it, mine flashes all the time.
How come you missed to mention digital font in your tutorial?
oh I didn't think it was important. It's from Dafont.com
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!!!
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
@@AIAdev i dont understand but thanks for replay.
I figured it out, thanks a lot! You're the only one who answered me. Thank you very much.
Ikr right like what's the point of having a timer that won't stop when the level ends lmao
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();
}
}
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!
looks like you're forgetting to end a statement, or multiple statements with semicolons. ;
@@AIAdev Ah thanks!
i forget to do that offten