Countdown Timer In Unity - Easy Beginners Tutorial/ Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys and girls and welcome back to another video in this video we're gonna be taking a look on how to make a countdown timer in unity this is very simple and it's great for beginners so let's get started first thing I want to do is create a new C sharp script that I'm gonna call countdown timer and here we're going to need a few variables so let's just create them we're going to need a float for our current time and we're also going to need a float for our starting time we can make this public you don't have to I'm going to go in void start and just say current time equals to starting time this is very simple so once the game starts we're going to set our current time equal to starting time let's just up here set our current time to zero and our starting time to ten we're going to go down to avoid update and now each second we want to decrease our current time by one we can do that by going current time minus equals one but this is going to do it each frame we don't want that we can use time.deltatime to fix that so this makes it so it decreases it by one each second I'm going to leave a link to time that Delta time documentation down in description this is all great let's just print our current time each frame I'll just create a temporary empty object this is only for testing so let's just drag our script here you can see it's decreasing by one that's all great but now we have to delete this object and now we want to display this on our screen we're gonna use UI text for that I'll just rename this to countdown text I'm going to increase my font size to something like 74 and now you can't see it that's because our rectangle is too small to display our text you can fix this in two ways you can either change your width and height of the rank rectangle or you can set it to overflow the rectangle down here I think it's better to change the rectangle transform so I'm just gonna do that and I'm also going to change my text to something like zero zero that doesn't matter because we're going to overwrite it if it doesn't look centered just go to your position and change that to zero zero and you can also go down here to alignment and set this to middle now it should be aligned and let's go to our canvas and drag our countdown text to it but we still are not changing our countdown tag text in the script so we want to do that so in the script we're basically going to change this value right here to our current time so let's go to our script and set it to use unity engine dot UI and now we can create a new variable and call it text so this is going to be this text object right here our countdown text and we're going to call it countdown text we're going to make it public actually let's make it serialize field this is same as private but only we can see it in our editor right here as you can see let's drag our countdown text to this and now instead of printing our current time we're going to set our count down text dot text so that is the text object on here the sorry the text component on here and then we're accessing this thing right here so countdown tags dot text equals current time but if you try to run this you will probably get a error message saying float cannot be converted to string that's all fine we can fix that by going current time that to string and put some parentheses here I almost forgot but I have this cool font right here I'm just gonna drag it over and I will leave a link to this font in the description you don't have to use it it just looks cool if we click plate you can see that our Korean time get displayed here but we only want to see the whole numbers we can also fix that pretty easily so we want to in the parentheses here just type this and now if you click play it should only display the whole number that works great but if you take a look you can see that we can go even into negative numbers so if we go down here and just type in if current time minus equals so you're not minus equals but if it's less or equal to zero we want to set our current time equal to zero so when the current time reaches zero it's just going to keep setting it to zero so we countdown at it doesn't display the negative numbers that's all great and I have a little task for you guys at home using this method you can set your text to change color depending on which number it is and I'm just going to show you a little example to change color you can go countdown text dot color equals two color dot and then you can choose to choose your color or you can go up here and create a valuable variable for the color so this is just a little task for you guys at home that want to learn a bit more so I would suggest maybe once the countdown reaches I'm sorry the current time reaches three maybe change the color to red so it kind of indicates that it's it's close to zero or something just have fun with it and if you do the task comment down below I just want to see how you guys did it so hopefully you guys enjoy this if it helps you be sure to leave a like comment down below on things that you want me to make videos on and go follow me on my social media there's a link to that in the description and also subscribe because it helps me out a lot hopefully I'll see you next time bye bye
Info
Channel: Single Sapling Games
Views: 157,505
Rating: undefined out of 5
Keywords: script, make, game, for, free, 2018, development, dev, challenge, unity, blender, paint.net, paintnet, gimp, mono, develop, hacknplan, hack, plan, program, tutorial, guide, new, hd, twitter, youtube, itchio, itch.io, developer, indie, design, art, how to, how, to, create, countdown, count, timer, time, in
Id: o0j7PdU88a4
Channel Id: undefined
Length: 7min 10sec (430 seconds)
Published: Sun Jul 01 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.