Below is a cheat sheet we're using on my team to help us access Game Object and Components from code. Hopefully it will come in handy for others!
Accessing Game Objects and Components in Unity
*Game Objects* should be thought of generally as items that show up in the Hierarchy view. They are meshes and physical objects (from Maya or made in Unity), lights (not attached to a specific Game Object), cameras, and GUI objects.
*Components* are those items that are attached to Game Objects, thus appearing in that Game Object's Inspector view. Components are colliders, lights (attached to an object), renderers, shaders, emitters, audio items, and animation clips (if the animation was part of an imported object).
Keep in Mind
The object-hierarchy of Game Objects and Components in code is subservient-to and dependent-upon the relative relationships of items to one and another in the Hierarchy view. Scripts are not generally able to see each other at a global level, instead needing to be accessed as any other tangible Component attached to a Game Object within a larger parent-child hierarchy.
Accessing Game Objects
*** Accessing the "Self" Game Object ***
Often a script needs to be able to access the Game Object to which it is attached. Rather than simply declare a public variable of type GameObject and drag the reference from the Hierarchy, use the following code to make the Game Object handle automatically within the script by passing the attached Game Object's name to the Find method.
*** Accessing the Parent Game Object ***
There are times when a script attached to a child Game Object needs access to the Components of it's parent Game Object. By extending the example above with the following line of code, you make a handle to the parent Game Object. Note that to access the parent Game Object, the "transform" property must be invoked and that to do so, we pass that parent's name property into the Find method.
*** Accessing Sibling Game Objects ***
Because they are at the same level of the hierarchy, sibling Game Objects can be accessed using the example code from "Accessing the "Self" Game Object" above. Remember that the principle here is that Game Objects grouped under a parent Game Object are siblings AND so are Game Objects parallel each other at the top level of the Hierarchy view. Tricksy Unity.
*** Accessing Non-Script Components ***
Once you have a handle for a Game Object in your code, you can manipulate and access the Components much as we would expect to do in any language. The following example accesses a renderer Component and changes the material's shader property. Note that you still need to create the initial reference to o_foo using one of the techniques above. Refer to the individual Component's references in the Unity Manual to get the exact syntax as it's not always what you expect.
*** Accessing Script Components ***
Accessing scripts attached to a Game Object, thus being able to read their public variables and utilize their public methods, is a little more complicated as a second handle needs to be made to the script itself. The following code assumes that we are accessing a sibling Game Object's (called "sibling_a") script called someScript and that we do not have a handle for that sibling yet.
Alternatively, if we already have a handle to the Game Object that has the script we want access to, we could can do the following.
- var o_script : someScript;
- o_script = o_foo.GetComponent(someScript);