Methods in C#

3D Game Design with Unity

🕑 This lesson will take about 7 minutes

A method, also known as a function, is a module of code that a programmer can create and then call on later on in the program. Many methods already exist in programming languages such as C# but the programmer also has the ability to make their own. A method will usually perform a single task. Many methods can work together to achieve a goal.

In Unity, the Start method contains code that runs before the first frame and just once. The Update method contains all of the code that runs every single frame of the scene.

This is the basic syntax of a method:

<return type> <name>(<parameters>)
{
// code that runs inside the method goes here
}

Methods should have descriptive names (they should represent an action and are usually in the form of a verb). Spaces cannot be used in method names and you should always avoid the use of special characters eg. $%^!@. The method name itself should also not contain brackets because brackets are used for parameters. When you create a new method, the method name should be unique and not already exist in the language (it should not be a reserved word that is used for a statement or method).

Watch the video below to learn how to create and use methods in C# and then scroll down to see the sample code.

Is YouTube blocked at school? Watch on Google Drive.

Creating methods

Let’s break up the method into its different components and look at each component…

What is a return type?

Methods are able to return a variable back to the code that called it known as the return type. If a method returns an integer variable then the return type is an int and if a method returns a true or false value then the return type is a bool. Even if the method doesn’t return any value, it still has a return type. If the method doesn’t return a value, then its return type is void (which means nothing). You might notice that the Start and Update functions have a return type of void.

What are parameters?

In the same way that methods can pass a variable back to the code that called it, the calling code can pass variables into the method. These variables are known as parameters. The variables that are passed into the method are identified in the parameter list part of the method (inside the brackets). When you specify a parameter you must specify the variable type and the name. If there are no parameters, then the brackets are left empty.

Below is an example of a method in C# for a game in Unity that is used to apply damage to a player’s health. There are two parameters in this method (separated by commas). The parameters in this method are damageAmount and health. Notice that the return type is int.

int applyDamage(int damageAmount, int health)
{
 return health - damageAmount;
}

The above method is a simple example. In an actual game, the player’s health could instead be stored in a variable inside a script so it wouldn’t need to be read into a method. However, the example above shows the structure of methods.

Using methods

Once you have created a method the next thing to do is use it. Using a method is known as calling or invoking a method. To call a method that was named myMethod, you would write:

myMethod();

If the method contained a parameter (eg. the value 10), it would be called like this:

myMethod(10);

The below example shows how to call a method and pass a variable into the method. You do not need to write int inside the brackets where the function is called.

int x = 10;
myMethod(x);

When you call a method you do not need to provide the variable type with the variable that is being passed into the method. If the method myMethod() in the example code above returns a value, then it should be stored in a variable, for example:

int result = myMethod(x);

Here is another example of the applyDamage() method: