Game Development Reference
In-Depth Information
Unlike the Start() function, AddFive() needs to receive information in order to process it by adding
five. Within the parentheses is the parameter numberFromStartFunction that is declared as an integer
data type. This means that when this function is called, it must be sent an integer. You already
declared myVar as an integer, so you are correctly sending the AddFive() function an integer data
type in your AddFive(myVar) function call. numberFromStartFunction works like a variable within the
AddFive() function to hold the integer received from the caller.
The first statement of the AddFive() function is the following:
var newVar : int;
Here you are declaring a new variable named newVar of type integer. You may recall from the
breakdown of a function described previously that you do not have to assign a value to a variable
when you declare it.
newVar is declared within the AddFive() function so it can only be used within this function. This is
an example of a local variable , since its scope is limited to the function in which it is declared. myVar
is an example of a global variable; with a much broader scope, it can be utilized by any function in
this script.
The second statement in this function, newVar = numberFromStartFunction + 5; , is the actual
processing of your instructions, where 5 is added to the integer received in the parameter
numberFromStartFunction . The result of this calculation is then stored in the local variable newVar .
Last, return newVar; returns the value assigned to newVar where it is assigned to myVar .
This process is depicted in Fig 2-23 . Run (save and play) the script and, sure enough, Unity gives
you a result of 13. Yay!
var myVar : int = 8 ;
function Start () {
myVar = AddFive(myVar);
Debug .Log(myVar);
}
function AddFive (numberFromStartFunction :
int ) {
var newVar : int ;
newVar = numberFromStartFunction + 5 ;
return newVar;
}
Figure 2-23. Flow of data between two functions
 
Search WWH ::




Custom Search