C# : Methods (part I)
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
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
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
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:
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!