Methods are where you define the instructions you want your script to execute.
The description in the above link is slightly misleading regarding development in Unity because while the Unity Engine itself does presumably use a Main
method internally as the main entry point for the application as described, your scripts will instead use various MonoBehaviour Messages which are methods you can define that Unity will execute in response to certain events.
Also note that the first two sentences on Unity's MonoBehaviour
documentation page as of this writing are false ("MonoBehaviour is the base class from which every Unity script derives. When you use C#, you must explicitly derive from MonoBehaviour."). Scripts that you want to attach to a GameObject
in the scene do need to Inherit from MonoBehaviour
, but there are lots of situations where you do not need to do so.
Calling
Executing a method is referred to as "calling" it.
Methods are executed sequentially from the top down one step at a time. Calling another method will execute it entirely before continuing to execute the calling method. For example:
using UnityEngine;
public class Example : MonoBehaviour
{
// A Field.
private int _Counter;
// _Counter starts at 0.
// MonoBehaviour Messages do not need to be public for Unity to call them.
// void is the Return Type.
private void Awake()
{
// The ++ Operator increases the value by 1.
_Counter++;
// _Counter is now 1.
// Call the AddSix method (see below).
AddSix();
// _Counter is now 7.
_Counter++;
// _Counter is now 8.
}
private void AddSix()
{
// _Counter is now 1.
// The += Operator is the same as writing "x = x + y".
_Counter += 6;
// _Counter is now 7.
}
}
Parameters | Parameters allow variables to be passed into a method by the caller. |
Flow Control | Instead of executing every instruction once each in the same order every time the program is run, you will often need to control the flow of the program to only do things under certain conditions or repeat the some instructions until a condition is met. |
Return Values | Return Values allow the method to pass a value back to the caller. |
Constructors | Constructors are a special type of method which can be used to define how instances of a particular class or struct are created and to initialize their starting values. |
Properties | Properties are special methods which appear to be used more like variables instead of being called like regular methods even though they are actually methods internally. |
Overloads | Methods can be overloaded by declaring multiple methods with the same name but different parameters. |
Messages | Messages are methods with specific names that Unity will automatically call when certain events occur. |
Delegates | A delegate is a variable which references a method. |
Extension Methods | Extension Methods allow you to "add" methods to an existing type without modifying or inheriting from it. |
Conditional Compilation | Conditional compilation allows parts of a script to be selectively removed by the compiler under certain circumstances. |
Expression Bodied Members | Expression Bodied Members are a shorter syntax for writing a method if it only contains a single expression. |