Game Development Reference
the variables of the class can be set to create differing objects. Each object
is called an instance of the class. In this case, setting the variable values for
Square allows for a variety of Square objects to be created. So although they
all look different, they are still squares and retain the essence of a square,
which is to have four equal sides and 90° angles.
The functions of an object can be used to set the values of the variables or
change the behavior. For example, the Rotate function in the Square class
might update the rotation variable and thus change the orientation
of the object.
Unity Hands On
Step 1. Download Chapter One/objects.zip from the Web site, unzip,
and open in Unity. In the Project, double-click on ChangeObjects to
open the scene. The Scene will appear empty. Attached to the Main
Camera is the script called createObjects.js . Play the file. A capsule will
appear in the Game.
Step 2. Open createObjects.js in the script editor. Note that gameObj
is a private variable created to hold a Game Object. In the Start()
function, it is assigned a PrimitiveType.Capsule . The capsule is a
complex game object where the attached components are also
objects. For example, Transform is an object. Position , which is a part
of Transform , is also an object. 5 Visualizations of the Game Object,
Transform, and Position classes, along with their locations in
the Unity Editor, are shown in Figure 1.33 .
On the Web Site
Game Object Definition
All of the variables and functions attached to a Game Object are listed
in the script reference at http://unity3D.com/support/documentation/
Step 3. To change the location of the capsule you can access the x , y ,
and z position coordinates via the Transform component of the Game
Object. Modify the script to that in Listing 1.27 . Save and play.
5 Actually, Position is a structure . This is another data type with complex variables and
functions. It acts so much like an object that we will just treat it as one rather than