Games Programming with C# in Unity

The If Statement

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This segment takes a look at the “If” statetement and, in particular, what it can do in creating the timer.

Keywords

  • Conditionals
  • Timer
  • Start Function
  • Update Function
  • Frame
  • Code
  • SceneManager

About this video

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

Video Transcript

In this movie, we’re going to look at the if statement or a conditional statement to see what they can do for us. In particular, when we’re counting down with our timer, there’ll come a point where it expires. This if statement can help us check when the timer expires and then handle the response.

In the preceding movie, we created our timer script, so that inside the update function we continually reduce the time remaining. We count down the Time Remaining variable. In this video, we’re going to take a look at two aspects that follow on from this. We’re going to look at conditional statements, and we’re also going to take a look at functions and see how these can work together to really enhance our scripts.

I’m going to start by taking a look at conditional statements. These are if statements. These are the kinds of statements that ask or check a particular variable and ask what is the case. In this instance, for example, we have our Time Remaining variable. We want to say that if the time remaining is 0 or less, then the time has expired. Let’s take a look at how we could do that.

We know at line 20, the Time Remaining variable is reduced, meaning we’re going to have less time on the line afterwards. I’m going to write a conditional statement to check the value of the Time Remaining variable. We want to ask in this instance, if the Time Remaining variable– or we could say if it equals 0, and that’s how we would write that.

But actually I want to ask if it’s less than or equal to 0 because it could be 0, but it could also be minus 1, for example. So I want to ask if the value is less than or equal to 1. And if it is, then the code that is between these braces here is going to happen.

Remember, this code, the code between the braces, is only going to happen if time remaining is less than or equal to 0. In other cases, it won’t happen at all, only if time remaining is less than or equal to 0. So if that is the case, at least, initially, I’m going to say that we should in fact print the message, not “Hello World,” but “time is up,” like so. So I’m going to print the message “time is up.”

I’m, going to save the script file and then return back to Unity here, clear the console, and I’m going to press Play on the toolbar. Now, I set the max time variable to 5 so that we only have to wait 5 seconds for this countdown to happen. And when it does happen, you can see here it prints inside the console, time is up.

So it actually does check what the value of that variable is, and then it performs the correct code, or that is, the time is up part. Now, when the time is up, what I want to do is I want to restart the level. I want to begin the level again. The player has failed to complete or to collect all of the time, all the coins inside the time, and so I need to restart the level. So I’m going to be doing that in my very own function.

We’ve seen here that we have the Start function and the Update function. These are functions that Unity gives us at particular times inside the level. Start happens when the level begins. The Update function happens every single frame. I’m going to make my own one.

I’m going to type void, and I’m going to call this level restart. You can call this anything you’d like– it’s your own function. And inside the Level Restart, I’m going to use a command that is going to restart the level. Now, to access this command, I need to go all the way up the top to this section here which tells Unity the range of different functions and commands that we have at our disposal.

I’m going to increase this to UnityEngine dot SceneManagement and then move down to the Level Restart function. At this point, I’m going to type SceneManager dot LoadScene. And then inside here, I’m going to say SceneManager dot GetActiveScene BuildIndex, like that. That’s the command that we can use to restart a level. Now, the exact details of how that works is beyond the scope of this course, but this is the command that we can use to restart a level.

So I effectively want all of the level restart functionality to happen when the time remaining is less than or equal to 0. So I’m going to grab the Level Restart function, the name of that function. I’m going to copy that and paste that into that section. Now, when Unity encounters a statement like this– this is called a function call– when Unity encounters a statement like Level Restart, it’s going to jump down to the Level Restart function and then execute whatever is between the two braces here.

I’m going to save this code and minimize Visual Studio here, clear this statement, got to press Play on the toolbar. And once again, we’re going to get the maximum time of 5 counting down. And then eventually we’re going to get to 0, and it’s going to restart the level as it has done here. In fact, if I continue to move around here, it’s going to restart me if I walk around again. And eventually, the timer is going to expire, and it’s going to restart me once again.

So now we have the Level Restart functionality up and running inside our timer. This timer is now doing everything that we need it to do. It’s going to count down from the maximum time. And when that timer is reached, it’s going to restart the level. And that is pretty much it for the timer.

Let’s go back to Visual Studio and see that code. When you think about it, there’s actually very little code inside this script file. There aren’t really many lines at all– at most, 33 lines.

And really, when you think about that, we’ve got lines like this which are just empty space. So they’re doing nothing at all. Unity will eventually ignore all of this empty space. So in hardly any lines at all, we have a complete timer that is working.

Now, it doesn’t display yet inside the user interface. Notice that the time left section, this part here, doesn’t actually show us how much time we have left. We’re going to be taking a look at how we can do that in the next movie.