Game Development Reference
In-Depth Information
localPosition versus position
The Transform component exposes two main position members:
position
and
localPosition
. Setting either of these will change
an object's position in specific and distinct ways. The position member
always defines an object's position in world space as a measure from
the world origin. Setting this variable in script, therefore, might not
correspond to the numbers you actually see for the Transform component
in the Object Inspector when the object is selected. If your object is a child
of another object that's not positioned to the world origin, for example,
then Unity would offset the object's local position away from its parent,
by however much is necessary, to position it at the world space location
that is specified. The
localPosition
member, in contrast, corresponds
directly to the
position
value shown for the Transform component in
the Object Inspector. Specifically, it specifies the position of an object as
a measured offset away from its parent location or else from the world
origin if the object has no parent. In the latter case, both the
position
and
localPosition
members will be identical.
More information on the
GetComponent
function can be found in
the online Unity documentation at
http://docs.unity3d.com/
You can also access the Unity documentation from the MonoDevelop
Help
menu, by navigating to
Help
|
Unity API Reference
.
Getting multiple components
Occasionally, you'll want to retrieve multiple components in a list: sometimes, a list
of all components and sometimes, a list matching only a specific type. You can get
this using the
GetComponents
function. See the following code sample 3-2. As with
the
GetComponent
function, it's good practice to call
GetComponents
during one-off
events such as
Start
and
Awake
as opposed to frequent events such as
Update
:
01 using UnityEngine;
02 using System.Collections;
03 //-----------------------------------------------------
04 public class MyCustomComponent : MonoBehaviour
05 {