Game Development Reference
In-Depth Information
Variable data types
Each variable has a data type. A few of the most common ones include
int , float , bool , string , and Vector3 . Here, are a few examples
of these types:
int (integer or whole number) = -3, -2, -1, 0, 1, 2, 3…
float (floating point number or decimal) = -3.0, -2.5, 0.0, 1.7,
3.9…
bool (Boolean or true / false ) = true or false (1 or 0)
string (string of characters) = "hello world", "a", "another
word…"
Vector3 (a position value) = (0, 0, 0), (10, 5, 0)…
Notice from lines 06-08 of code sample 1-1 that each variable is assigned a starting
value, and its data type is explicitly stated as int (integer), string , and Vector3 ,
which represent the points in a 3D space (as well as directions, as we'll see). There's no
full list of possible data types, as this will vary extensively, depending on your project
(and you'll also create your own!). Throughout this topic, we'll work with the most
common types, so you'll see plenty of examples. Finally, each variable declaration line
begins with the keyword public. Usually, variables can be either public or private
(and there is another one called protected , which is not covered here).The public
variables will be accessible and editable in Unity's Object Inspector (as we'll see soon,
you can also refer to the preceding screenshot), and they can also be accessed by
other classes.
Variables are so named because their values might vary (or change) over time.
Of course, they don't change in arbitrary and unpredictable ways. Rather, they
change whenever we explicitly change them, either through direct assignment in
code, from the Object Inspector, or through methods and function calls. They can
be changed both directly and indirectly. Variables can be assigned values directly,
such as the following one:
PlayerName = "NewName";
They can also be assigned indirectly using expressions, that is, statements whose
final value must be evaluated before the assignment can be finally made to the
variable as follows:
//Variable will result to 50, because: 100 x 0.5 = 50
PlayerHealth = 100 * 0.5;
 
Search WWH ::




Custom Search