Unity Tutorial : Creating a Timer using a slider

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how's Langa's welcome back to another unity tutorial so in today's video I'll be creating a timer that is within a slaughter it's pretty cool because that Ashley roms but to show you how it works if I just run this so I've set the time to be ten seconds so you can see the slider slowly ticks down as well as the Tama slowly ticks down and then until it gets to zero cool so that's it let's get into it so I've created a empty senior and the first thing we want to do is just add our slaughter so get you our slaughter I will just create our slider on the screen we then we just want to maybe just move it so it's in the correct position this increase the size a little bit just make sure that looks like a cool that looks good what you might also want to do is on the canvas itself is just to change this you our scale mode for example if this is on a mobile device the screen size might be a bit different and you can see the slider counter just just disappears off the screen so what you want to do is just change this to scale the screen size this will fix this the slider to one position cool so once we're done with that we then when I just go to slaughter we want to delete this handle slattery up and really need that anymore and there's one more thing we need a change on the slider itself is this weird issue where for example if you change the max value to one or the values fold to one you can see there's always this little area that's lecture so we need to fix that so let's just take this down to zero again go to this full on the width that Sophia you want to just change the to zero then on the full area let's just go to the rect tool zoom in a little bit here and just wanna make this full cool so we can test that let's just go back to our game I didn't change the Stradivari here ah there we go so you can see it's not really that little gap that will put the area that wasn't getting folded it's not getting pulled in cool so the next thing I wanna do is just our text area just a text and I just use the rect tool to adjust this a little bit we can change the Tama text and we can change this to zero zero the reserve I'm just a little bit make it a little bit smaller don't it's so large and that's just just at such sits perfectly within our slaughter cool that looks good the next thing what I do is just create an empty game object and this we're going to call our game console so we're going to be adding our script onto this and this is where our timer will be controlled so the next thing I want to do is just create a new folder let's call the scripts and inside here we're going to create our new C shop script called Tama once you've created double-click and open up your Studios the first thing I want to do is say using Unity engine dot you are this is to allow us to use text so we're going to first say public slaughter farmer slaughter so we where we define our slaughter what slide are we gonna be changing the ready for next we wanna say public text this will be the for the timer text then we want to say public floats game time so be our obviously the game time and then we want to say private to stop timer this is so when we know when to stop the timer' so on the void let's just say you could make this set to false for now we also then want to change the max value and the actual value of the slider so let's just say Tom slaughter max value is equal to game time so this you have to set the max value first in order to change the value just put an S there Tom slaughter dark value come on value is equal to game time cool so that's going to set the value and the max value to whatever the max game time is for example one minute 60 seconds it will set the max page 60 so the next thing we want to do is find a float Tom and we're gonna equal this to game time - Tom Tom so if you're working with one minute for example 60 seconds we're gonna be reducing one second off the game time every give you updates so the next thing we want to do we want to get the amounts of minutes in our game time so all you'll do is minutes equal Mac if starts floats to int open brackets Tom divided by 60 so this is how we will get the amount of minutes that we have in our game in a game time then you want to get the amounts of seconds same thing math if Bolton's - minutes 60 qu so that's how we get the amount of seconds that we have left then we need to use the minutes and seconds to put it in some kind of black format so we're gonna say text Tom is equal to string dots formats open bracket open quotes curly brackets there are three records one that will be the text Tom the format and you want to say minutes and seconds so we know using minutes and seconds we didn't want to say if the time is less than or equal to zero then we're gonna set the stop timer give it a false true sorry so that's when we know when to stop the timer' and then if stop timer leave it to false then Tom a text text text Tom Andrea so when I said to Tom tada dot value equal to Tom cool that's all we need to do for the Thomas script let's get back into unity also in unity now we need to drag our timer script on to our game console game object we just want to drag the slider to the Thomas ladder we're gonna drag a time add text yeah Emmy can just change this to 64 now obviously one just change this full area to like a red so just looks a bit better cool so if I run this let's see that's gone to 60 and you can see it's slowly going down yeah that's a Gus if you have any questions or anything just drop a comments if you like this video please leave a like and subscribe it really does mean a lot and yeah see you guys in this one shows you [Music]
Info
Channel: CatoDevs
Views: 19,030
Rating: undefined out of 5
Keywords: timer, slider, unity tutorial, tutorial, unity, unity how to create, how to, create a timer, sliders, unity slider, unity timer, unity slider timer, timer using a slider, C#, game development, simple, youtube, how, time, timer slider, slider timer, slider tutorials, how to slider, slider tutorial, simple timer, game timer, game timer slider, timer in a slider, slider act as a timer, slider as a timer, tutorials, cato, game engine, unity game engine, simple tut, unity tut, tut, code
Id: S12x7txHS1c
Channel Id: undefined
Length: 8min 4sec (484 seconds)
Published: Tue Jun 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.