Just as with variables, we have to let Unity know about a method before we can use it. Depending on who you talk to, some will say "We have to declare a method," others will say "We have to define a method," or even "We have to implement a method." Which is correct? In C#, it doesn't make any difference. Use whichever term helps you learn more easily. I like to say I'm defining a method's code block, nothing like declaring a simple variable on a one-line statement.
There are three minimum requirements for defining a method:
returnDataType NameOfTheMethod ( ) { }
Looking at LearningScript
once again, or any Unity-generated script, you can see that the Start()
method has the three minimum requirements for a method:
void Start () { }
Here's what we have:
void
keyword is used. This informs Unity that nothing is being returned from the method.Start()
.This example fulfills the bare minimum requirements for a method. However, as you can see, there's no code in the code block, so when Start()
is called by Unity, it doesn't do anything at all. Yet it's a method. Normally, if we aren't going to use a method by adding code to a skeleton method created by Unity, we can simply remove them from our script. It's normally best to remove unused code after the script has been written.
Here's what we know about this bare-minimum method definition as far as Unity is concerned: