Methods in C# – Programming for Unity Game Development – Game Development Blog

“It is the method behind the madness that makes it madness.’

― Marty Rubin

Do you want to be a game developer? Learn C# programming fundamentals in Unity with this free tutorial by @MammothCompany! All about methods….Where would we be without them?

In this tutorial:


To follow along with this tutorial, you’ll need to open a new Unity project.

Sure, Unity gives us two default methods in each new script we make. But to make a game, we’re going to need a lot more than that.

Part 1: What Is A Method?

A method is a task or function that you can
reuse throughout code simply by calling its name.

But before you can call a method, you must declare it and set it up, including any parameters (data that the method will use to perform its task) or return values (the value that the method will spit out when it has completed its task, usually to determine that value.)

Making a new method also allows you to
reuse it easily by simply calling its name.

The best way to learn is by doing. So, let’s make a new method for a Unity game.

Part 2: Making a C# Method

Open the script you made that controls your game, typically called GameController.cs. If you don’t have a Game Controller yet, check out our previous tutorial.

Type the following line below the Update method to create a method named PrintMessage.

void PrintMessage () {


The method’s name is PrintMessage. Notice
that the first letter of each word in the name begins with a capital letter,
but the remaining letters are lowercase. This is the language convention you
should follow whenever creating a method in C#.

void means
that the method will not return a value. Because the parentheses are
empty, the method receives no parameters. There is no return statement using
the return keyword, so (at least for now) the method does not return any value.

The code we put between the curly brackets
is the scope of the method. This is where we write the task that we want
the method to perform whenever its name is called.

Let’s call the Debug class’s Log method to print a string and the sum of two variables we created in the previous tutorial.

void PrintMessage () {
Debug.Log ("The sum is: " + (number1 + number2));

Now, whenever we call PrintMessage (), the
Log method inside PrintMessage () will be executed as we specified.

Part 3: Testing Methods in Unity

Save the script, and open Unity. Press
Play. Only “Let’s go!” will print in the Console. This occurs because
we did not call the PrintMessage method. We need to not only create a method
but also call it.

Press Stop. Open GameController.cs. Use the following code to call PrintMessage in the Start method, along with the message variable we created last tutorial.

void Start () {
Debug.Log (message);
PrintMessage ();

Now if you save the script and run the
game, the Console will print the value of message and the value of PrintMessage.

Part 4: Setting Method Parameters in C#

What if we want to give PrintMessage
parameters? Delete the number1 and number2 variable lines from the
GameController class. Instead, use the following code to set number1 and
number2 to be parameters of PrintMessage.

void PrintMessage (int number1, int number2) {
Debug.Log("The sum is: " + (number1 + number2));

This changes PrintMessage to receive 2 integers as input, rather than setting the integers on the developer side.

Part 5: Handling Errors in Unity

If you save the script and play the game
now, you will receive the following error message in the Console:

error CS1501: No overload for method ‘PrintMessage’ takes ‘0’ arguments

This message means that in the
GameController.cs file, at line 12, PrintMessage has no parameters when it
should. Because we created PrintMessage with parameters, we need to call it
with the same parameters. Use the following format to pass 20 as the number1
parameter and 22 as the number2 parameter.

void Start () {
Debug.Log (message)
PrintMessage (20, 22);

This passing of parameters is like passing
message as the parameter of the Log method.

Now if you save the script and play the
game, the Console will print “Let’s go!” and “The sum is

Part 6: Building a Message

Suppose we want to build a message rather than print it. Let’s try this out! Rename PrintMessage “BuildMessage”. Replace void with string. Our method will now return a value as a string rather than an integer.

string PrintMessage (int number1, int number2) {
Debug.Log ("The sum is: " + (number1 + number2));

Instead of calling the Log method to print the sum, use the keyword return to return the message. With the return keywod, we can use the method’s return value anytime by calling the method by name.

string PrintMessage (int number1, int number2) {
return ("The sum is: " + (number1 + number2));

In the Start method, replace the
PrintMessage call with a Debug line. Call Log with BuildMessage as a parameter.

Give BuildMessage the parameters 1 as the value for the number1 parameter and 2 as the value for the number2 parameter.

void Start () {
Debug.Log (message);
Debug.Log (BuildMessage (number1, number2));

Calling BuildMessage () will return its
return value.

Let’s test our new method! Save the script, and open Unity. Press Play. The Console will print “Let’s go!” and “The sum is 3”.

Press Stop, and don’t forget to save your project.


—Team Mammoth from Mammoth Interactive INC. Tutorial by Glauco Pires and Transcribing by Alexandra Kropova

More resources on this topic:

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *