Game Development Reference
In-Depth Information
Variables are considered in more depth in the next section.
More information on creating and using scripts in Unity can be found
online at http://docs.unity3d.com/412/Documentation/
Manual/Scripting.html .
Variables
Perhaps, the core concept in programming and in C# is the variable. Variables often
correspond to the letters used in algebra and stand in for numerical quantities, such as
X , Y , and Z and a , b , and c . If you need to keep track of information, such as the player
name, score, position, orientation, ammo, health, and a multitude of other types of
quantifiable data (expressed by nouns), then a variable will be your friend. A variable
represents a single unit of information. This means that multiple variables are needed
to hold multiple units, one variable for each. Further, each unit will be of a specific type
or kind. For example, the player's name represents a sequence of letters, such as "John",
"Tom", and "David". In contrast, the player's health refers to numerical data, such as
100 percent (1) or 50 percent (0.5), depending on whether the player has sustained
damage. So, each variable necessarily has a data type. In C#, variables are created
using a specific kind of syntax or grammar. Consider the following code sample 1-1
that defines a new script file and class called MyNewScript , which declares three
different variables with class scope, each of a unique type. The word "declare" means
that we, as programmers, are telling the C# compiler about the variables required:
01 using UnityEngine;
02 using System.Collections;
03
04 public class MyNewScript : MonoBehaviour
05 {
06 public string PlayerName = "";
07 public int PlayerHealth = 100;
08 public Vector3 Position = Vector3.zero;
09
10 // Use this for initialization
11 void Start () {
12
13 }
14
15 // Update is called once per frame
16 void Update () {
17
18 }
19 }
 
Search WWH ::




Custom Search