Time for action – adding code between the parentheses
We're going to modify LearningScript
to send some information to the AddTwoNumbers()
method to make it much more useful.
Why would we need to send information to a method?
A script may need to add two numbers several times, but they probably won't always be the same two numbers. We could possibly have hundreds of different combinations of "two numbers" to add together. This means that we need to let the method know, which two numbers need to be added together at the moment when we call the method.
Using the preceding screenshot, perform the following steps:
Open
LearningScript
in MonoDevelop to modify it.Add lines 6, 7, and 8 to declare three integer variables.
Add lines 22 to 26 to define the
AddTwoNumbers()
method with parameters.Add lines 12, 13, and 14 to call the
AddTwoNumbers()
three times.Save the file.
Click on Play in Unity.
What just happened?
As this script executes, the AddTwoNumbers()
method is called three times on lines 12, 13...