Game Development Reference
In-Depth Information
one name. The capital makes the variable name more readable as single
words are easier to read. A variable name could also be written
account_Balance
using the underscore to separate words. This is a totally personal preference
and makes no difference to the compiler; however, naming conventions 4
are recommended. For example, if programming in JavaScript, camel casing
for variable names is recommended with the first letter being in lowercase.
Because conventions can vary slightly between languages, it will not be
covered in this topic.
JavaScript Variables
Variables in Unity JavaScript usually appear at the top of the code file.
Although they could be placed in a variety of locations, the best place
is at the top because they are easy to find. We will examine other
types of variables in later sections.
Consider the script in Listing 1.10 .
Listing 1.10 JavaScript to Change the x Axis Scale
of a GameObject on Game Start
private var objScaleX: float = 0.5;
function Start ()
{
transform.localScale.x = objScaleX;
}
The line showing the variable declaration is shown in bold . Note, do not use
bold in your own code; this is for illustrative purposes only. The keyword
private forces the variable to remain hidden for use inside the JavaScript file
in which it is declared. It cannot be seen for use by other code files. You can
add many JavaScript files into the same Unity application, and sometimes
you will want to be able to share the variable contents between these. In
this case, you would declare an exposed variable. In this example, the code
would be the same as Listing 1.10 , except the private keyword would be
removed.
When a variable becomes exposed, it also appears in the Inspector of
the Unity Editor where values can be entered manually. The difference
is illustrated in Figure 1.26 .
4 For more information about naming conventions, see http://en.wikipedia.org/wiki/
Naming_convention_(programming ).
 
Search Nedrilad ::




Custom Search