Game Development Reference
In-Depth Information
06 //Reference to all components as array
07 private Component[] AllComponents = null;
08 //-----------------------------------------------------
09 // Use this for initialization
10 void Start ()
11 {
12 //Gets a list of all components attached to this object
13 AllComponents = GetComponents<Component>();
14
15 //Loops through each and list it to the console
16 foreach(Component C in AllComponents)
17 {
18 //Print to console
19 Debug.Log (C.ToString());
20 }
21 }
22 }
23 //-----------------------------------------------------
More on Components
Unity offers additional variations on the
GetComponent
and
GetComponents
functions that facilitate interobject communication
and not just communication between components in the same object.
These functions include
GetComponentsInChildren
to retrieve
an accumulative list of all components in all children and also
GetComponentsInParent
to retrieve all components in an
object's parent.
More information on the
GetComponents
function can be found in
the online Unity documentation at
http://docs.unity3d.com/