this.Hi

Hi all,

I have been working with Unity for about 3 months, from scratch. I have no experience in this field at all, but I have aspirations and drive.

The problem I am finding with Unity forums, is that if you search you will find the answer to everything and more ! So in reality I have actually learned, scraped and and tutorilised approximately 280 hrs in these few months and have yet to find and actual need to post on the forum. You can see the problem.

So I am saying hello and thanks for all the answers to questions and advice I have received thus far without typing a single character.

That being said I have a question now, I have been working on JavaScript externally so I wish to remain working with it. I have noticed command lines on the forums where people have answered questions often using a syntax like this.

var playerPos:Vector3 = playerObject.transform.position;

now I tend to write it as follows, assuming of course the script is attached to the object I am referencing, which works fine.

var playerPos:Vector3 = this.transform.position;

So the question is, is there any inherent dangers using this. command that may rear it’s ugly head later on?

Again thanks for all the answers.

No, it’s fine. Your first example is a little confusing, since that is not necessarily equivalent to your second line, but I sense that you get that.

Using “this” in that particular context is what we call “syntactic sugar.” It can be helpful for clarity, but which way is actually more clear is pretty subjective. You can leave “this” out and just say “transform.position” and there’s no difference. Personally I use “this” fairly often, as it can be a nice reminder of the scope.

1 Like

That’s great thank you.