x


Having an object reference itself

I am still fairly new to Unity and programming in general. I have been having trouble comparing information between objects and the object that I have attached a script to. I would like to access all of the information for an object if it has a particular script attached to it. Is there any specific way to have an object reference itself or is there a programming technique I should use to make sure an object holds onto that information?

more ▼

asked Feb 02 '10 at 08:06 PM

RandomMeasure gravatar image

RandomMeasure
34 4 6 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

you can get a reference to the gameobject running the script itself by

this.gameObject

to see if a GameObject has script x attached you should try to use GetComponent and it will retun null if there is no component of type x attached.

if (GetComponent(x) == null)
{
//no component of type x attached
]
else
{
//component is attached
}
more ▼

answered Feb 02 '10 at 08:30 PM

Ashkan_gc gravatar image

Ashkan_gc
9.3k 33 56 120

Thanks! this.gameObject was exactly what I was look for. I assume "this" can be used to reference other aspects of the object that the script is attached to.

Feb 03 '10 at 05:52 PM RandomMeasure

Not exactly. The "this" refers to the script itself. So if you have, say,

var foo : int = 0;

function bar (foo : int) {
    this.foo = foo;
}

What you're saying is "Set the variable foo in this script to be the foo that gets passed in as an argument to the function. "this" happens to work with gameObject because gameObject is defined in MonoBehaviour ( http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html ).

Feb 25 '10 at 09:33 PM burnumd

@burnumed7 please use the @ notation when you want to answer another's question in my answer. i could not figure it out why you should tell this to me. @RandomMasure this keyword is a reference to current instance of the class that you are writing. at runtime this keyword is the class (script) that is executing. you can use this keyword and GetComponent to access anything you want in your gameObject.

Feb 26 '10 at 04:47 AM Ashkan_gc
(comments are locked)
10|3000 characters needed characters left

To access components between objects, use the GameObject.GetComponent family of methods (see also GetComponentInChildren, along with GetComponents and GetComponentsInChildren, which return an array of those components, if there are multiple instances of a component attached to an object). Eg:

var myComponent : ScriptType = targetGameObject.GetComponent (ScriptType);
if (myComponent) {
    //do things with myComponent
}

var myComponentArray : ScriptType[] = targetGameObject.GetComponents (ScriptType);
for (var component : ScriptType in myComponentArray) {
    //Do something with an individual instance of that script.
}

Where "ScriptType" is the name of the script (or other component, like ParticleEmitter) you're looking for and "targetGameObject" is the GameObject you expect to have the component (for example, the "other" in an OnTriggerEnter or what have you). Remember GetComponents always returns something while GetComponent will return null if the script isn't attached to the object you're checking.

more ▼

answered Feb 02 '10 at 09:10 PM

burnumd gravatar image

burnumd
3.4k 22 35 71

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x276
x150

asked: Feb 02 '10 at 08:06 PM

Seen: 3947 times

Last Updated: Feb 02 '10 at 08:06 PM