Game Development Reference
In-Depth Information
The string is the characters contained within the double quotes. Concatenation is the term for
putting together individual strings into a single string. While I could have said that you can “add”
strings together, it is important for you to learn the proper terminology as it is introduced, which will
come in handy for performing a forum search on Unity Answers when you need help, or for sounding
really smart at cocktail parties. Edit your code to the following:
#pragma strict
// declare the variable myVar of type integer and give it a value of 8.
var myVar : int = 8;
var myString1 : String = "Hello ";
var myString2 : String = "World!";
var myNewString: String;
function Start () {
myNewString = String.Concat(myString1, myString2);
Debug.Log(myNewString);
}
Save, play, and your output should be “Hello World!” The most common mistake here is to forget
about including spaces where you need them.
String.Concat resembles Debug.Log in that they both contain a dot operator . The dot operator is a
form of coding syntax, where the dot separates words of code. Concat is an element of String , just
as Log is an element of Debug . You can use the now-familiar +' on String and Debug to see more of
their elements in the Unity Scripting Reference.
The Start function has worked well for demonstrating these basic concepts, but now you are ready
to write your own functions. A function accepts information, called arguments , and performs some
kind of action with the information, then optionally returns a result.
Delete the string variable declarations and edit the Start function as follows:
var myVar : int = 8;
function Start () {
myVar = AddFive(myVar);
Debug.Log(myVar);
}
The statement myVar = AddFive(myVar); calls the function named AddFive() that you will create
next, and sends it the integer stored in myVar .
After the close brace of the Start() function, write the new function called in Start named AddFive :
function AddFive (numberFromStartFunction : int) {
var newVar : int;
newVar = numberFromStartFunction + 5;
return newVar;
}
 
Search WWH ::




Custom Search