Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Creating switch statements
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to learn about switch statements. All right, so inside of Unity, let's go ahead and create a new lesson script. Let's Create C# Script. And let's go ahead and call this lesson_07. And double click on that and let's bring on MonoDevelop here. All right, so let's start out with a brand new script here. And what I'm going to do is I'm going to create a new variable right here inside of public class. And let's put it right here on the left side, and I'm going to call this public. And then we're going to give it its data type, which is going to be integer. And we're going to give it an identifier or a name, which is going to be weapon. And we're going to set this equal to zero. All right, so now that we have our weapon variable initialized here, what I want to do is I want to come into my update function. And I want to create a random number using that variable. So starting out, let's go ahead and type in weapon = random.range. And this is going to create a random number within a range that I specify. So to specify the range, let's open parentheses and we're going to 1, which is going to be our minimum number, and it has to be an integer as you can see here. And then we're going to set our maximum number, which is going to be five. And let's end that with a semicolon. Now let's go ahead and create our switch statement. So you want to type in Switch. And in our switch statement, we need to pass in a variable. Now, this variable has to belong in the same function that the switch statement is in. So as you can see, we initialized weapon up here at the top, so that way we could access it inside of Unity. And then we just use that variable inside of our update function. And it's now being used as a random number. So switch, and then we're going to type in weapon. And now we need to create our cases. So let's open curly brackets for our switch statement. And a case is basically a value that is going to be used by the variable in here. So we can have a case from 1 to 5, because we're going to be using these values here. So let's use case 1. And let's give it some code to actually execute in case the switch statement lands on the random number 1. So we're going to say debug.log. And let's give it a little message here. So let's say "You found the sword." We'll end that. And with every case, we have to break it, so that way it knows where to stop executing code. So what we're going to do is we're going to type in break semicolon. And now we can move on to our next case. So case 2, we want to do debug.log. And our message is going to be "You found the axe." If I could type today. There we go. And then we're going to break that as well. And let's move on to case 3. So case 3, let's go ahead and copy the debug code here. So Control-C and then Control-V. And let's say "You found the dagger." And let's move on to case 4. And we're going to type in debug.log. And we're going to say "You found the bow." Now, I know I could have pasted that in there, but I was already halfway finished so no reason to. Let's go ahead and break that. And then with every switch statement, there needs to be a default case that it can go to. Now, a default case is kind of like the else statement of your if statements. So if you'll remember that from the last lesson. So we're going to type in default colon, and then tell it what to do if none of these numbers are being generated. So debug.log. We're going to say, you need to find a weapon. And then we're going to break that and there we go. So let's go ahead and save this and go into Unity. And you'll notice that we're getting an error here. Now, inside of Unity, whenever you see an error, you want to go ahead and double click on that. That will bring up your console. But more importantly, it will highlight the error in your script. Now, you can see here that we're not highlighted on any sort of code. So a parsing error really has to deal with your curly brackets. So we've got one that's mismatched here. So starting at the top, we have our class open curly bracket. And I know that this is supposed to be my class closed bracket. We have move-- or I'm sorry, our void start. And then we have our update. OK, now looking here, we have our switch that we opened but we didn't close that. So right here on 35, let's hit Tab one time. And let's close that curly bracket. So now those are matched up. Let's go ahead and save this. Go into Unity. And you'll see that we have another error. And this is saying control cannot fall through from one case label to another. So looking at this, let's go ahead and double click on that. And you'll see that it's having a problem with our switch statement. So the issue that's happening here is it's going through the cases, and then it's being interrupted It can't fall through all of the different cases. So we have case 1 is set to break, case 2 is also set to break. But then case 3, we do not have that. So let's make sure that we type that in. So break, and then semicolon. Let's go ahead and save that, go into Unity. And you'll see that all of our errors are now gone. Let's go ahead and close this out. And let's assign lesson 7 to our game object. So let's remove that old component, add lesson 7. You'll see that weapon is now set to zero. And whenever we hit play, you'll see that that number generates randomly. And you'll see that we have a message being typed in in our console. So this is working properly. Now, we have been able to put everything in our start functions and in our update functions. And we know that the update function runs every single frame and the start function only runs at the beginning whenever the game is started. So what happens whenever we have code that we only want to call at certain times? Well, what we'll have to do is we'll have to actually create what's called custom functions. And we're going to learn how to use those next.
In this series of Unity tutorials we'll discuss the major foundations of scripting with C# in Unity and apply what we've learned in two mini projects.

To start out, we'll look at several of the terms and techniques that are used when scripting in Unity such as creating and manipulating variables, understanding the different types of operators, and how we can create instructions for our game objects using functions. We'll also jump into creating logic with conditional statements, and loops. We'll even learn how to use basic arrays. Finally, we'll take what we've learned and apply it to creating a movement and animation script.