Games Programming with C# in Unity

Events and Functions

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment shows how to work with key Unity events, how to create and customize functions.

Keywords

  • Unity
  • Events
  • Start
  • Update
  • Functions
  • Customize
  • Timer
  • Variable
  • Update
  • SerializeField

About this video

Author(s)
Alan Thorn
First online
31 October 2019
DOI
https://doi.org/10.1007/978-1-4842-5567-4_3
Online ISBN
978-1-4842-5567-4
Publisher
Apress
Copyright information
© Alan Thorn 2019

Video Transcript

In this movie, we’re going to see how to work with key Unity events like start and update and how we can create functions and use functions to customize behavior. In particular, we’re going to see how we can create a countdown for the timer. In the preceding movie, we set up the variables for our time object.

So we have this max time variable here. This tells us the total amount of time that the player has the compete level. And that’s looking great.

But of course, we now need to count down from that time. We want to take max time, which is 60 seconds. And over the course of time, we want to reduce that to eventually zero. And then, of course, when we reach zero, the level is over.

How can we do that? Well, we can use that in combination with these functions here. Remember this is a function, and it represents a space inside which our code can do stuff. So in this example, start, this is going to happen once when the level begins. So the level begins, and anything inside start is going to happen once and only once.

Here we have the update function, and this is really quite different. In fact, not only is it quite different, but it is critically important inside Unity. The update function– this is going to occur once every frame. What does that mean in practice? Well, inside the space of just one second, your game could perform perhaps 60, 70, 80, even 100 frames per second.

So within the space of a second, you can squeeze in 100 frames– maybe even more. That means this update function could happen a lot, and I mean a lot. For that reason, we want to be careful about how much we stuff inside this update function. If you put too much inside it, you will bring your game to a complete crashing halt. But in the case of a simple timer, we don’t really need to worry about that.

So inside the update function, what I want to do is since this is happening regularly, repeatedly over time, we’re going to be using this function to reduce the value of max time. For example, I could take max time, and I could say, well, max time equals itself minus 1. Let’s take a look at exactly how that works.

This is going to happen repeatedly, and let’s think carefully about that. The very first time this happened, the statement max time, which initially begins at 60, is going to equal itself, which is 60 minus 1 hence is going to be 59. Now next time the update function happens, max time has already become 59 is going to equal itself, again which is 59 minus 1 which is now 58 and so on. That means that every time update is going to happen, max time is going to reduce by 1.

Let’s try again and see how it looks inside the Object Inspector. I’m going to reduce the coding window and go back to the Unity scene here, back to our glorious coin collection world, and I’m going to press Play on the toolbar.

Now keep your eye on the max timer field here because this is going to move like lightning. I’m going to press the Play button here, and straight away the max time field is reducing. And it’s moving so fast.

It’s even gone all the way into negative numbers. And you know what this is never going to stop as long as I live here. This is going to reduce and reduce down to quite significant values.

So you can see that it really reduced way, way, way down into negative numbers. It didn’t take the process of a second to do that. It didn’t take even the process of a minute to do that.

Within the space of just a few seconds, this was already in negative numbers. And the reason this is happening is, of course, because the update function is happening more than simply 1 per second. It’s happening many times per second. And so many times per second max time is being reduced.

So how can we reduce this so that after one second, max time will have reduced by 1? And the way we can do that is we can use a special variable that Unity has. So the variable is not the one here because that’s a literal variable that we have put in, but we can put in the statement time dot delta time. And this is a value that Unity gives us. I’m going to save this here and go back to Unity, allow my code to compile.

So the time dot delta time variable tells us how much time has passed since the previous frame in seconds. This will allow us to reduce our code so that over the course of one second, the time value will have reduced by 1. I’m going to press Play on the toolbar. And now when I do this– actually, I pressed play twice there– make sure I get it right.

When I press Play here, you can see that the timer script here is reducing, and now it is actually reducing consistently with time. So I’m going to press Control-P or Command-P to stop playback, and I’m going to go back here to our script file.

Now there is something happening here. My code is not highlighting correctly. So what I’m going to do is I’m going to temporarily pause recording. I’m going to close Visual Studio and load that back because I’m expecting this line to be highlighted slightly differently, and the fact that it is not is a little bit suspicious.

So I’m going to close the window here, and I’ll load that back in just a second. OK, so I’ve loaded back my code, and now you can actually see here that the highlighting is different for time here. It now recognizes that as a keyword.

So we have our line that reduces the time here. So we’re having update code continuously many times per second, and max time is being reduced. And we can always check out its value here directly in the Object Inspector. Now in this instance here, we can see that these start function is never really being called. Nothing is really happening with the start function here. So I’m simply going to select that entirely and delete it.

Now one of the problems that we have with our code right here is that max time is continually reducing. And when we think about it, that’s not really what we want. Max time is constantly supposed to tell us the maximum time that the player had to complete the level. We’re not supposed to reduce that because if we ever wanted to reset the level back to its original value, we would have forgotten what that value was because max time would have reduced to zero. We actually want to keep this value intact to tell us how much time we actually have, even when the timer is reset, and we want to create a separate variable that’s going to keep track of the amount of time we actually have left.

So I’m going to create a second variable, which is called time elapsed or in fact, time remaining. And it’s initially going to equal the max time. So actually, I’m going to need the start function again because when the level begins, I want to say that my time remaining equals the maximum time allowed. And inside the update function, rather than reduce max time, I’m going to be reducing the time remaining.

Now you’ll notice that I’ve set this variable to private, meaning that it cannot be shown inside the Object Inspector. Now in this instance, I actually do want it to display inside the Object Inspector. So I’m going to set that to public, or rather, I could set that to public.

If I really wanted to keep it private and to force Unity to show it inside the Object Inspector, you can use this special attribute command called serialized build. When you put this on the line above the variable name, it will force that variable to be shown inside the inspector. I’m going to save this code with file and save or Command-S on a Mac and then go back to Unity here.

When I select the timer script, you can see here that I see both a max time and the time remaining. When I press Play on the toolbar, the time will reduce from 60, and you can see that max time stays constant because that’s the maximum time allowed, but that time remaining is reducing. Great, that works excellently.

So we’ve now created a script file that reduce the max time. This is a great start. We can reduce max time.

But now, of course, when the time actually runs out, we want to do something. We want to end the level or to restart the level. We’re going to take a look at how to do that in the next movie.