Research & Development for 3D & the Web

Unity – Accessing Components

by on Apr.25, 2010, under Unity, Unity Scripts

Straight from the Unity Site here:

————————————————————-

Components are attached to game objects. Attaching a Renderer component to a game object is what makes it render on the screen, attaching a Camera turns it into a camera object. All scripts are components thus they can be attached to game objects.

The most common components are accessible as simple member variables:

Component Accessible as
Transform transform
Rigidbody rigidbody
Renderer renderer
Camera camera (only on camera objects)
Light light (only on light objects)
Animation animation
Collider collider
… etc.

For the full list of predefined member variables see the documentation for the Component, Behaviour and MonoBehaviour classes. If the game object does not have a component of the type you wish to fetch, the above variables will be set to null.

Any component or script attached to a game object can be accessed through GetComponent.

transform.Translate(0, 1, 0);
// is equivalent to
GetComponent(Transform).Translate(0, 1, 0);

Note the case difference between transform and Transform. The former is a variable (lower case), the latter is a class or script name (upper case). This case difference lets you to differentiate variables from class&script names.

Applying what we have learned, we can now find any script or builtin component which is attached to the same game object using GetComponent. Please note that to make the following example work you need to have a script called OtherScript containing a function DoSomething. The OtherScript script must be attached to the same game object as the following script.

// This finds the script called OtherScript in the same game object
// and calls DoSomething on it.
function Update () {
otherScript = GetComponent(OtherScript);
otherScript.DoSomething();
}

——————————————————————-

More on getting components from Game Objects from the Unity Site here:

GameObject.GetComponent

 

function GetComponent (type : Type) : Component

Description

Returns the component of Type type if the game object has one attached, null if it doesn’t. You can access both builtin components or scripts with this function.

GetComponent is the primary way of accessing other components. From javascript the type of a script is always the name of the script as seen in the project view. Example:

function Start ()
{
var curTransform : Transform;curTransform = gameObject.GetComponent(Transform);
// This is equivalent to:
curTransform = gameObject.transform;
}
function Update ()
{
// To access public variables and functions
// in another script attached to the same game object.
// (ScriptName is the name of the javascript file)
var other : ScriptName = gameObject.GetComponent(ScriptName);
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the the other script instance
other.someVariable = 5;
}

For C# users there is a generic version available.

public class Something : MonoBehaviour
{
void Update()
{
//notice you don’t need to cast here.
ScriptName sn = gameObject.GetComponent()
sn.DoSomething();
sn.someVariable=5;
}
}

 

function GetComponent (type : string) : Component

Description

Returns the component with name type if the game object has one attached, null if it doesn’t.

It is better to use GetComponent with a Type instead of a string for performance reasons. Sometimes you might not be able to get to the type however, for example when trying to access a C# script from Javascript. In that case you can simply access the component by name instead of type. Example:

function Update ()
{
// To access public variables and functions
// in another script attached to the same game object.
// (ScriptName is the name of the javascript file)
var other = gameObject.GetComponent(“ScriptName”);
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the the other script instance
other.someVariable = 5;
}
:programming, script, Unity

Leave a Reply

You must be logged in to post a comment.

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...

Log In