Difference between Keyword "this" and "gameObject"

From what I know, “this” points to the script attached to the paddle object. “gameObject” is the object that the script attached to. I don’t think “this” and “gameObject” are interchangeable, as “Destroy(this)” deletes the script and “Destroy(gameObject)” delete the object the script attaches to.

In 77. Movement By Mouse. There is a line of code:

  • this.transform.position = paddlePos;

First I don’t understand this code. does “this.transform” indicate the x/y/z coordinates of the script? (a script can be moved around?)

Secondly, I modified the code into:

  • gameObject.transform.position = paddlePos;

It also works the same as the previous line by changing “this” to “gameObject”. I am confused why they are interchangeable in this context.

1 Like

For what transform is specifically, click on a gameObject in unity that’s in the scene. On the inspector you can see the components and the top one should be the transform component. In this it has the position, rotation and scale properties. So for this.transform you are referring to the entirely component then the X/Y/Z position in the world is this.transform.position which is a Vector3 so holds them in (x,y,z)

‘this’ is a keyword that refers to the class that is running the code. It’s never required as far as I’m aware but makes your code more readable this.transform.position and transform.position will do the same thing as Unity reads the second like it was the first.

gameObject.transform.position will return the position of the gameObject that the script is attached to so it returns essentially the same position and really all 3 are going to give you the same position.

However one important part to keep in mind the difference is when you wish to destroy an object. Destroy(this); will destroy the instance of class but not the object itself whereas Destroy(gameObject); will destroy the entire object including the instance of the script.

1 Like

I did some research, and I think I understand now. My confusion is that I don’t know the method “Component.transform” and “Component.gameObject”:

The hierarchy is like this:
Game Object < contains > components (such as transform, collider, Script etc)

According to the API documentation:

  • Component.transform: points back to the gameObject the component is attached to.
  • Component.gameObject: points to the Transform attached to this GameObject.

We know “this” refers to the script. therefore:

  • “this.gameObject” points back to the Game Object that contains the script.
  • “this.transform” points to the Transform attached to this GameObject.

Because “this” is not required, so “this.gameObject.transform” also can be written as “gameObject.transform”. Therefore “gameObject.transform” and “this.transform” points to the same tranform component.

But Destroy(this) only deletes the script, Destroy(gameObject) deletes the object

1 Like

I would agree with everything you have posted here and your understanding, but would make two subtle distinctions;

According to the API documentation:

Component.transform: points back to the gameObject the component is attached to.
Component.gameObject: points to the Transform attached to this GameObject.

Component.transform points to the Transform component of the GameObject, the component is attached to
Component.gameObject points to the GameObject the component is attached to

We know “this” refers to the script. therefore:

this.gameObject” points back to the Game Object that contains the script.
this.transform” points to the Transform attached to this GameObject.

this.transform points to the Transform component of the GameObject, this script is attached to.

In Unity, because the GameObject’s Transform component is accessed so frequently, it is given effectively a shortcut to access it, e.g.;

gameObject.transform

…where-as for other components attached to the GameObject you would use;

gameObject.GetComponent<AudioSource>();

GameObject is a base-class, but it’s constructor always creates a Transform component, so you never get given a GameObject which doesn’t have one.

The actual class hierarchy would look like this;

  • Object
    • GameObject
      • Component

Both Component and GameObject inherit, separately, from Object. A GameObject may have many components.


See also;

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms