Time for action – learning how a method works

We're going to edit LearningScript again. In the following screenshot, there are a few lines of code that look strange. We are not going to get into the details of what they mean in this chapter. We will discuss that in Chapter 4, Getting into the Details of Methods. Right now, I am just showing you a method's basic structure and how it works:

  1. In MonoDevelop, select LearningScript for editing.
  2. Edit the file so that it looks exactly like the following screenshot.
  3. Save the file.
Time for action – learning how a method works

What's in this script file?

In the previous screenshot, lines 6 and 7 will look familiar to you; they are variables just as you learned in the previous section. There are two of them this time. These variables store the numbers that are going to be added.

Line 16 may look very strange to you. Don't concern yourself right now with how this works. Just know that it's a line of code that lets the script know when the Return/Enter key is pressed. Press the Return/Enter key when you want to add the two numbers together.

Line 17 is where the AddTwoNumbers() method gets called into action. In fact, that's exactly how to describe it. This line of code calls the method.

Lines 20, 21, 22, and 23 make up the AddTwoNumbers() method. Don't be concerned about the code details yet. I just want you to understand how calling a method works.

Method names are substitutes too

You learned that a variable is a substitute for the value it actually contains. Well, a method is no different.

Take a look at line 20 from the previous screenshot:

void AddTwoNumbers ()

The AddTwoNumbers() is the name of the method. Like a variable, AddTwoNumbers() is nothing more than a named placeholder in the memory, but this time it stores some lines of code instead. So anywhere we would like to use the code of this method in our script, just write AddTwoNumbers(), and the code will be substituted.

Line 21 has an opening curly-brace and line 23 has a closing curly-brace. Everything between the two curly-braces is the code that is executed when this method is called in our script.

Look at line 17 from the previous screenshot:

AddTwoNumbers();

The method name AddTwoNumbers() is called. This means that the code between the curly-braces is executed.

Note

It's like having all of the code of a method right there on line 17.

Of course, this AddTwoNumbers() method only has one line of code to execute, but a method could have many lines of code.

Line 22 is the action part of this method, the part between the curly-braces. This line of code is adding the two variables together and displaying the answer to the Unity Console. Then, follow the ensuing steps:

  1. Go back to Unity and have the Console panel showing.
  2. Now click on Play.

What just happened?

Oh no! Nothing happened!

Actually, as you sit there looking at the blank Console panel, the script is running perfectly, just as we programmed it. Line 16 in the script is waiting for you to press the Return/Enter key. Press it now.

And there you go! The following screenshot shows you the result of adding two variables together that contain the numbers 2 and 9:

What just happened?

Line 16 waited for you to press the Return/Enter key. When you do this, line 17 executes which calls the AddTwoNumbers() method. This allows the code block of the method, line 23, to add the the values stored in the variables number1 and number2.

Have a go hero – changing the output of the method

While Unity is in the Play mode, select the Main Camera so its Components show in the Inspector. In the Inspector panel, locate Learning Script and its two variables. Change the values, currently 2 and 9, to different values. Make sure to click your mouse in the Game panel so it has focus, then press the Return/Enter key again. You will see the result of the new addition in the Console.

You just learned how a method works to allow a specific block of code to to be called to perform a task.

We didn't get into any of the wording details of methods here, this was just to show you fundamentally how they work. We'll get into the finer details of methods in Chapter 4, Getting into the Details of Methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141