[QUESTION] The difference between this, gameObject and no reference at all

Hi guys! Great course, I am having a lot of fun playing with unity. Just as a short background I am JavaScript developer on a daily basis and I decided to write all the stuff with C# (which I don’t know) just to take a break from JS.

What I find I little bit confusing is a lot of variables which are inherited/exposed from some of the upperscope classes/global vars. As far as I understand most of them are Unity specific. My question is what is the difference between those:

this.transform.foo
gameObject.transform.foo
transform.foo

Are this and gameObject references to the same thing (which would be an instance of current class)? If not what is the difference between them and to which one I am referencing when I skip this/gameObject completely?

Hopefully that’s comprohensible question. I was looking into docs but perhaps I have overlooked answer to this question. If someone could point me to the correct place in docs or answer shortly, that’d be great.

I know it perhaps deasn’t really matter at this point of the course, I am just curious :slight_smile:

Thanks in advance.

Ps I’ll post my pimped version of Block Breaker here soon :slight_smile:

1 Like

Hi @nefil1m,

Good question! I came from a .Net background but for VB.Net and I initially found it confusing to get my head around how things were being exposed seemingly here there and everywhere. :slight_smile:

Regarding the specific question;

Shorter and longer ways of writing the same thing from my perspective.

Using Visual Studio and the following code in a Start() method on the Ball.cs script, some observations…

Debug.Log(this.transform.ToString());
Debug.Log(gameObject.transform.ToString());
Debug.Log(transform.ToString());

All three outputted lines to the console were identical. Ball / transform etc.

Visual Studio also reports that I can tidy up my code by removing the this.

For me it feels like a combination of short hand/long hand of writing the same thing.

The script itself is, if attached to a GameObject as per the above example a component of the GameObject. Therefore this. should refer to the GameObject as a whole. Saying this.transform would be referencing a Transform component attached to the same GameObject.

Using gameObject were are referencing the GameObject the current script component is attached to, so gameObject.transform would again reference the Transform component of the GameObject.

Using transform directly, again is referencing a Transform of the GameObject that the script component is attached to.

It is implied that every component must be attached to a GameObject, I believe (I could be wrong so happy to be corrected) that this implication derives from the use of : MonoBehavour at the top of the class, it is the base class all of the Unity scripts derive from.

I am sure it would be possible to use one of the above versions and add other code which may cause some form of ambiguity, maybe by adding another sub-class with the same name (not sure that’s even possible).

I personally don’t tend to use this. nor do I use transform., I prefer gameObject.transform - primarily because it’s very clear as to what I am referring to in my code, should anyone else ever read it (hope not! :wink: ) Of course with the modern IDEs where you just hover over something and it tells you exactly what’s what I guess some of my approaches are redundant these days…

Not sure if the above really helps, hopefully so… :slight_smile:

3 Likes

@Rob Thanks for your answer, it clears a lot :slight_smile:

What was my main concern is that I initially thought this refers to the class instance, while gameObject to some-kind-of-unity-stuff-which-also-uses-this-instance :wink: But since it is not, all my doubts are now gone.

1 Like

You’re more than welcome. I wiill look forward to seeing your games posted in the Showcase in due course :slight_smile:

Hi again,

During implementing lives system in my game I found that there is a small difference between this and gameObject. Not sure what the difference exactly is, but I decided to post it here anyway in case someone would find it interesting.

I have a function like this:

void OnTriggerEnter2D (Collider2D col) {
	Destroy(gameObject);
}

Which correctly destroys object as desired. However this:

void OnTriggerEnter2D (Collider2D col) {
	Destroy(this);
}

won’t destroy anything.

I’m posting this just in case someone would have any troubles with destroying objects :wink:

Hey there @nefil1m,

From my understanding, “this” is the script itself and refers to the GameObject instance as a whole like Rob said, but it is not the gameObject, it just refers to it, so you have to say Destroy(this.gameObject) or just Destroy(gameObject) in order to destroy the GameObject, by saying Destroy(this) you are destroying the script.

Personally, I just don’t use “this” at all, but I`ve heard that there are some situations, where you have to use static variables and singletons, that “this” can help to specify that you are relating to a specific instance of the gameObject, but I have never used it.

3 Likes

Privacy & Terms