- Home /
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?
Answer by Ashkan_gc · Feb 02, 2010 at 08:30 PM
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
}
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.
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 $$anonymous$$onoBehaviour ( http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.html ).
@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. @Random$$anonymous$$asure 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.
Answer by burnumd · Feb 02, 2010 at 09:10 PM
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.
Your answer
Follow this Question
Related Questions
Multiple objects with the same script having trouble accessing another script/object 1 Answer
How Can i Reference String another Script? 2 Answers
How to run function in another script with prefabs? C# 2 Answers
Best way to reference another same object 0 Answers
Better way of interacting and sharing variable between two scripts? 1 Answer