Important Notice:

The Digital-Tutors site will soon be retired and your access will be turned off - but don’t worry, you can still enjoy all your favorite Digital-Tutors content on Pluralsight (plus thousands of additional courses, skill assessments, paths and more). You can log in to Pluralsight with your Digital-Tutors credentials, as well as update your password at any time on Pluralsight. Please contact us with questions or concerns: support@pluralsight.com.
Creating custom functions
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we'll learn how to work with custom functions. All right, let's go ahead and create a new C# script. Let's call this lesson_08. And I'm going to open that up in MonoDevelop, and I'm actually going to take all the scripts from Lesson 7 and let's paste those into Lesson 8. So Control-C and Control-V. And any time that you paste something like that, you need to make sure that the class is the exact same name as the script that it's in. So let's save that, and let's get to work here. So in the last lesson, we had run into an issue. We had created all our scripts, all of our executions here, and directions in the update function. And what had happened is it would go ahead and update every single frame so it was running this code every frame, and it wasn't giving us any control. So to give ourselves a little bit more control is we can create some custom functions. So let's separate this code out. What I'm going to do is I'm going to go ahead and take my switch statement and this variable here, this statement, and we're going to cut that out, and we're going to create a new function. So let's type in void and the function name. So I'm going to call this WeaponSearch. Open closed parentheses and then we're going to type in Open Curly Bracket, hit Enter, Closed Curly Bracket, and then in between there I'm going to hit Control-V to paste in our code. So we have weapon=Random.Range and then our switch statements. All right, so now we have those, let's hit Save and go into Unity. Let's go ahead and make sure that Lesson 8 is on our GameObject. So let's remove that component, apply Lesson 8, and let's hit play. Now you'll notice that we're no longer getting that message. It's not displaying our log. And the reason that that is is because if you take a look, our update function is empty. The update function runs every single frame, whereas other functions like WeaponSearch the ones that we create, do not run until they are called. So this is actually a function definition. So let's type that in there. This is a function definition. So all functions must be defined with the instructions, and then they must be called. So in our update function, let's go ahead and call WeaponSearch. OK, open closed parentheses, and then semicolon. This is a function call. Let's save this. Let's go into Unity. And we'll hit play, and now you'll see that it's generating a random number just like it was before. All right, so we've done that. So how do we give our player a little bit more control? Well, what we can do is we come in here, and we could actually set up to where the WeaponSearch is only called when the player says so, and we could do that by hitting a button on the keyboard or something like that. So to do that, I'm going to go into my update function, and I'm going to type in if. Now if the player hits a button on the keyboard, then it's going to go ahead and call the WeaponSearch. So to do this, we're going to use what's called input, and this is a class that is in the API. So you could see input., and then we're going to tell it what to get. All right, so GetKeyDown. So GetKeyDown. Now, you could also use MouseDown or GetKeyUp or GetKey itself. And actually, let's use GetKeyUp because what this does is it waits for the player to press a button and then release that button. Now if you use GetKeyDown, you could actually hold it, and it will execute that code as long as that button is held down. And we don't necessarily want that. So GetKeyUp and then we need to tell it what key to actually get. So in this case, I'm going to type in "space"-- this is the code for the Space Bar. Now just like with open and closed curly brackets, you need to make sure that you have your parentheses paired together. So this is opened and closed, and this one is open and it must be closed as well. Let's go ahead and open curly brackets and around that WeaponSearch. Let's close curly brackets for that. Now if I hit Save and I go into Unity, let's hit play, and you'll see that nothing is happening. So it's waiting for us to hit that Space Bar. OK, now it's working, and I can continue to hit that. And you'll see that it goes ahead and it searches for that weapon. It's running that code. Now, I want to go ahead and I want to take these functions just a little bit further. We've got the major functionality of our code here, but what I want to do is I want to separate out my switch statement here. Every time I press a button, and that randomly generates this number. If we get case 1, it goes ahead and displays this message. I want to create a function that will actually display the message itself so I don't have to keep typing in messages like this to my switch statements. So this can get a little long, OK. So what I want to do is I want to create a new function. So just below the void WeaponSearch, let's go ahead and type in void DisplayMessage. Open closed parentheses and then open and closed curly brackets. All right, so inside of our display message, what I wanted to do is I want it to display a message from a weapon name. K, so let's go ahead and just type this out here. So I'm going to do debug.log(" and I'm going to say "you found the space, closed quotations, and then we're going to use the plus symbol to tell it to place in a variable. So I'm going to say weaponName+"!"); All right, so what I'm doing here is I'm replacing all of this text that we have to write in every single case statement. So now what I want to do is I want to go ahead and just remove that. OK, just making this a little bit easier and run a little faster. OK, so now what do I do? Well, I have this weapon named variable, and this is not going to work because I haven't initialized this variable, and I don't even know what it means in all of these cases. So inside of case 1, let's type in WeaponName= and I'm going to give it a string. K, so in quotations, I going to say "sword" and then end with semicolon. Let's go ahead and copy that, and let's go to case 2, and let's replace sword with axe. Let's go to case 3 and replace sword with dagger, and then case 4, we're going to do bow. And we're going to leave the default message exactly the way it is. Let's hit save. And if we go to Unity, we're going to get some errors. It's saying that weapon name does not exist. K, and what that means is we haven't initialized that weapon name variable. So what I want to do is I want to come up here to the top and let's go to this public int weapon=0 and just below that, let's create a variable. So we're going to call this public and we're going to be creating a string type for this weapon name. So we're going to type in string. And a string is just a word. OK, it's a string of characters. And we're going to give it the weapon name identifier or name, and we're to set this equal to something, K. Now, I can set it equal to a default string so I could actually say nothing, like so. But I don't necessarily want any information in this point. So just like with values, we set the integer to zero. So if I want to set the string to nothing, I'm actually going to type in null. All right, so now that has been initialized, let's go ahead and save this and run our code. OK, so if I hit play, you'll see that nothing is happening. Let me hit the Space Bar, and you'll see that weapon number has been generated, so 4 and then it's displaying that string name of bow. But it's not display my message here. So the issue that we're having here is we are not calling the display message function. So that needs to be called in our case statements. So we're going to type in debug-- or I'm sorry, display message and then we're going to call that function. Now what I needed to do is I needed to actually take the weapon name, in this case, and I needed to pass this value into my display message function, OK, so it replaces this name here. So what I need to do is tell it display message, and then it's going to take the weapon name. So it's going to take this variable right here, in this case, and it's going to take it to the display message function, OK, and place it right in here. Now if you're going to travel or basically ship variables from one function to another, you need to have the person that's calling it out to the person that's receiving it. So inside of display message, we need to tell it what it's actually receiving. So it's going to receive a string, and then the variable that it's going to receive is weaponName. Let's go ahead and copy this. It's a Control-C. Control-V. And we're going to do this all the way through here. And there we go. Let's go ahead and save that. Go to Unity. We're not getting any errors right now. So let's go ahead and save this. Let's hit the Space Bar and you'll see that we are now getting our message, and its corresponding to our integer that we get as well as the string that we're passing to our weapon name. Now, you'll notice that weapon name is being exposed here, and I don't want to be able to change that. It's not necessary to actually see that here. So let's go ahead and switch that from a public variable to a private variable. Now let's go ahead and go into Unity. We can hit play. That variable's now gone, but we can still hit the Space Bar, and we see our message being displayed. All right, so now in our next lesson, we're going to take a look at loops and how they can help us even further with our functions.
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.
Introduction and project overview
1

Introduction and project overview

 
00:56
Basic C# Terms
2

Basic C# Terms

 
12:02
Creating and manipulating variables
3

Creating and manipulating variables

 
07:50
Working with assignment and arithmetic operators
4

Working with assignment and arithmetic operators

 
08:11
Working with comparison and logical operators
5

Working with comparison and logical operators

 
11:17
Creating logic with if statements
6

Creating logic with if statements

 
11:38
Creating switch statements
7

Creating switch statements

 
07:23
Creating custom functions
8

Creating custom functions

 
11:59
Working with loops
9

Working with loops

 
11:22
Understanding arrays
10

Understanding arrays

 
16:00
Project: Basic move script
11

Project: Basic move script

 
07:26
Project: Refining the movement script
12

Project: Refining the movement script

 
08:53
Project: Creating the jump function
13

Project: Creating the jump function

 
09:54
Project: Refining the jump function
14

Project: Refining the jump function

 
08:39
Project: Finishing the jump function
15

Project: Finishing the jump function

 
06:45
Project: Creating the advanced move script
16

Project: Creating the advanced move script

 
13:48
Creating the jump functionality
17

Creating the jump functionality

 
05:58
Project: Scripting basic animations
18

Project: Scripting basic animations

 
08:16