Just like for 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. Which is correct? In C#, it doesn't make any difference. Use which ever term helps you learn easier. 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, the Start()
method has the three bare-bone minimum requirements for a method:
void Start () { }
Here's what we have:
void
is used. This informs Unity that nothing is being returned from the method.Start()
.This example fulfills the bare minimum requirements to be 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, but it's still 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 is done being written.
Here's what we know about this bare minimum method definition as far as Unity is concerned:
private
by default. Therefore, this method cannot be called from other scripts.One thing for sure is that it makes easy to recognize that it's a method, but why are they part of a method's name?
We already know that a method is a code block that is going to get called multiple times. That's one of the reasons why a method created in the first place, so we don't have to write the same code over and over. Remember the AddTwoNumbers () method back in Chapter 2. It was very simple method used to explain the concept of a method and how to call it. Now it's time to take the next step and learn the usefulness of the parentheses.
3.144.93.141