- Home /
How do you refer the Current Object
How can a script refer to the object that it is attached to? Or how can you refer to the you instance of the script you are in?
Like a "Self" identifier.
Answer by burnumd · Jun 17, 2010 at 10:31 PM
The instance of the script you're in is referred to here, as in many OO languages as "this," eg:
var x;
function Awake () { this.x = "Foo"; }
The "this" isn't strictly necessary in most cases unless you have variables with conflicting names that can only be resolved by their scope or you simply want to make it clear that you're referring to this something-or-other. More examples:
var foo;
function Bar (foo) { // We're assigning whatever argument we get in this function // to the class variable foo. this.foo = foo; }
or
function CopyTransform (other : GameObject)
{
//Typically, you would make this function take a Transform
// rather than a GO, but for this example...
this.transform.position = other.transform.position;
this.transform.rotation = other.transform.rotation;
}
You can also use "this" if you need to use the instance as an argument to another class' functions. Say you have a "Enemy" class. When that enemy gets destroyed, you want the player to know something or other about the enemy it just destroyed:
//Player Class function DestroyedEnemy (enemy : Enemy) { score += enemy.points; inventory.Add (enemy.droppedItem); }
//Enemy class var points = 100; var droppedItem : GameObject;
function OnKilledByPlayer (killedBy : Player) { killedBy.DestroyedEnemy (this); }
That's how you have an individual script refer to itself. As for the Game Object, all your scripts are technically MonoBehaviours (unless you specify otherwise in Javascript), and all MonoBehaviours inherit from a number of things, but most importantly, they inherit the gameObject property, which will let you know what GameObject your script is attached to.
var myGameObject : GameObject;
function Awake () { this.myGameObject = gameObject; }
You can do something similar with a script's Transform (with .transform
), which is very handy for optimization.
The "this" examples you referenced aren't really useful (or necessary), they just create unnecessary clutter. Typically "this" is used when you're passing a reference to the object you're currently in, to some external object, and it saddens me that you didn't even include that as an example. :(
Well now I'm sad that I made you sad. :( :( I'm doubly more sad than you. But you're right, it was an oversight to exclude that example, so I added it. I stand by including "this" examples, as the OP did ask how to refer to an instance of the script. Without any guidance as to what they wanted to do with it, I included the wall-of-text general primer on self-reference in the context of Unity/games in the hopes it might be useful to someone with a similar question.
In addition using "this" makes code reuse and refactoring easier because you can change "this.x" to "currentObject.x" easily and do it for all object parameters rather than looking for "x", "y", "transform", etc.
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Referencing booleans from settings 1 Answer
getting a variable from another script problem 1 Answer
How do you reference a GameObject in a C# script that is a part of that GameObject? 1 Answer
Setting Scroll View Width GUILayout 1 Answer