Unity Methods

Method in Unity Scripting

In the previous posts, I used methods like “Start” and “Update” several times and I said what their uses are, but I am going to tell you what the method itself is and what its uses are in Unity.

A method is sometimes called a function, but what you call it does not make a difference in its work. In the C# language, a method is a block in which one or more lines of code are written, and it does not do anything until it is called, and sits quietly in a corner and waits. Because you may call it at some point, it must have a name to understand that you are calling it to do something. Each method does a specific job, so it is different from other methods. It’s like you have several robots in a factory that sit in a corner and are activated by sound, as soon as you call one of them, it gets up and goes and does a task that was defined for it and returns to its place.

A method is defined once but can be used many times in different places. Suppose you have a few lines of code whose task is to determine what color the user has chosen in the game environment and fetch that color and assign it to the model the user chooses. If I want to simplify it again, for example, in the game environment, you want to ride a car and choose the color of the car yourself each time. You choose the color and the color of the car changes to your chosen color in the game environment. Now, if you want to do this without using the method, you have to copy those few lines where the user can choose the color. If you have three lines of code to do this color change and you want to set the color in ten different positions in the game, you have to copy thirty lines of code in total. More importantly, if you need to modify these codes later, it will be very difficult to change them. This is where the method comes in. You put three lines of code inside a method and just add the method name wherever necessary:

This is a method ()
{
What color did the user choose?
Change the original color of the car to the color chosen by the user
See if the user has a driver’s license 🙂
}

It can be said that the most important feature of the method is to prevent repetition, which also helps to easily edit the codes. Another feature of the method is that you can define it in such a way that it returns a value after the code inside it is executed. Or not at all, just does what it is supposed to do, that is, execute the codes inside it, but does not return any value. If you remember or have heard of an old Iranian tradition in which a person would cook a dish called ‍Aush and send it to his neighbor or family in a bowl. That neighbor or relative would not send the bowl back after eating or would put something like an apple in the bowl as a sign of thanks to that person and send it back. Now void has the role of that relative or a neighbor who is not supposed to send anything back. But if it is supposed to return a value, we must specify its type, whether that value is an integer, decimal, etc., and at the same time, we must use the word “return” inside that method, which is called a keyword. Its use is that sometimes you need to write a method that calculates something and use the result somewhere else.

This method does not return anything

void MyMethodTest()
{
    // Change the color of the car in the first stage of the game
    // and 
    // The last line of three-line code
}

This method returns an integer value. The returned type must be the same as the one you defined at the beginning of the method, otherwise, it will give the error CS0029.

int MyMethodTest()
{
    int firstValue = 1;
    int secondValue = 2;
    return firstValue + secondValue;
}

Another important point is access to the method. If we want to have full access to a method, we put the “public” keyword in the first line where you define the method. Now, if you want to assign it to a button inside Unity, it can be accessed, for example, see line 11 in this post. Below are two sample methods whose access is defined as “public”:

public void MyMethodTestA()
{

}

public string MyMethodTestB()
{
    return
}

But if you want the method to be accessible only inside the class in which you wrote the method and do whatever it needs to do there, you use the “private” keyword. With this, you can no longer access this method within the Unity UI or from another class.

If you do not specify access, that is, do not write “public” or “private” in front of the method name, your method will be defined as “private” by default. The following two method examples have no difference and both are “private”:

void MyMethodTest()
{

}

private void MyMethodTest()
{

}

In general, these are called Access Modifiers, and there are four others besides “public” and “private”, but these two are the most important.

The method is not supposed to always be simple and there are cases where you need to transfer information to the method so that the method can use them to do something. I will update this post soon with an explanation about the parameters and arguments in the method.

If you have any questions or comments, feel free to leave them in the comments section below.

Leave a Reply

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