C# : Methods (part I)

Matteo Lo Piccolo
3 min readApr 26, 2021

--

Before we go any further, what are the methods?

Now , to understand, I use this simple example.
If we want sum two numbers and see result, how can we do?

This is probably the easiest problem we can have, but we are solving problem, sooo, solve the problem!

The point is :
I have two numbers
And i need to sum this two numbers and read the total, ok?
Something similar to this

Really really easy right?
I have a = 5, I have b = 10, and I have total which is a + b, ok?
Now, we use a command called “Debug.Log” for printing messagges in the console.

This command ask a STRING type to print messages, so : o we create a variable and pass it in the method, o we write inside this “ “ the message that we want.

We can do this

In Start method we have Debug.Log(“Hello World”);

Go back in Unity and press Play
For testing this I use the same project of our game, and I create a simple script “Test” and attach to the camera. Nothing crazy or complicated.
Now, look at the Console

After Play, Console print the Debug.Log message

The difference between Start and Update is this :
“START” works only ONE TIME,
UPDATE works EVERY FRAME.
In fact, if we move the Debug.Log from Start to Update

And press Play

Update continue to print Debug.Log message

Ok, now.
If we want stamp one time, so in the Start, the result of the su of our two numbers, we can do in two ways:

Debug.Log(a + b)
or
Debug.log(result)

Same thing and same result.
In practice : we can pass the numbers and make calculation inside Debug, or store the result in a variable and pass that variable inside Debug.

So, Debug.Log is a method inside Unity and help us to print messages in the Console.
But we can create our methods!

For example, we want create our method to sum two numbers.
The syntax when we create our methods is this:

Under the Update method I create the Sum method

PUBLIC or PRIVATE as variables,
the word VOID or the TYPE of method (for example : int, float, etc),
NAME of method, by practice is ALWAYS uppercase and parentheses.
After that, open and close curly brackets.

In the next article we explain better every part of our methods!

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet