This. question

Hi,

I’m bit confused with ‘this’. So as far as I know, ‘this’ refers to name of the script you are in. But… let say if the script name was Ball, and we are doing

this.transform.position …

why can we not do below?

Ball.transform.position…

I feel like ‘this’ refers to gameobject name, but I did played with names but it certainly was referring to script name. Please correct me if I am wrong.

1 Like

.this refers to the instance of the class executing the script, by this.transform you are refering to the particular reference to the transform within this very class instance. It don’t refers neither to the gameobject or its name.

You don’t need to use this.transform in an non static context since the system would be using the instance reference either way( you can use just transform.position). this keyword is mainly useful to specify the instance you are referencing within a static method or static environment

1 Like

There’re uses for this inside constructors and/or non-static methods, specifically when a class variable name is hidden inside the scope of the method/constructor.

For example:

public class MyClass {
		
	int myInteger;

	public MyClass (int myInteger) {
		this.myInteger = myInteger;
	}

	public int Sum (int myInteger) {
		return this.myInteger + myInteger;
	}
}

Both in the constructor and method this.myInteger refers to the class variable, whereas myInteger to the parameter (scoped locally).

Another use for this is for method extensions, i.e.:

public static MyExtendedMethod (this int myInt, //other args ) {}

1 Like

Yes, in those cases it is also referencing the variable within the class instance. There is also some other usages for this keyword in other contexts such as making one constructor inherits from other such as

public MyCotr()
{
//something
}

public MyCotr(int id) : this()
{
//something else
}

The second constructor will call the structure within the first one and then add it own, so it would result on
//something + //something else

Thanks for explanation. Appreciated. I would like to clarify here several things.

.this refers to the instance of the class executing the script, by this.transform you are refering to the particular reference to the transform within this very class instance.

from your response here, " by this.transform you are refering to the particular reference to the transform within this very class instance. ", I quite don’t get it. What do you mean by “particular reference” here?

There could be a hundred of different instances of the same class, each one with a different value for each variable, by saying this.transform you are explicit saying that the reference to the variable “transform” is the one cached within the instance that is running the code during it execution (if there is 100 instances of the same class, for example 100 enemies within the game with the same class Enemy.cs attached, each one will have it own instance of Enemy.cs and it will run it separately).

But the thing here is that since it only have access to his own reference, it won’t make any difference to say .this.transform or just transfom, it would access it own instance of the variable either way.

Hi Joao_Dalvi,

Thanks for getting back. Before I dive into another question here…(sorry for bothering with these), can you explain the term ‘instance’ ?

1 Like

Sure, it is a pleasure to help.

Classes are a kind of “mask”/logic, and it kind of don’t exist until there is a “instance” of it. Imagine a kind of Job, a police officer for example, you know what someone with this profession is supposed to do, but the actual work won’t happen until there is someone actually working as a police officer, so if there is one officer named Jim for example, than you could say that Jim is one instance of Police officer, and if besides Jim there is other 5 officers, than you would have 6 instances.

The same happens to the classes, it depends on an instance so the code gets executed. Unless it is a Static method, those methods don’t need a specific instance, they exist within the class structure.

That is why when you want to declare a Vector3 you have to type “New Vector3(x,y,z)”, by doing so you are creating an instance of the class. But if you want to use Vector3.forward you won’t need to create it since the definition is static

I see, I grab the concept of Instance for sure:grinning:. Now, getting back to the original question,

by saying this.transform you are explicit saying that the reference to the variable “transform” is the one cached within the instance that is running the code during it execution

So here, what it means is that 'we are going to use instance that particularly has the variable “transform” ? And why does ‘this’ somehow equals to the script name?(as being described in the lecture)

Every instance will have the variable transform, you are just saying to use the variable inside the instance that is executing the code at the time (it will execute on all instances eventually, but code is read in a linear manner), this is the exact reason why it is redundant and useless (code wise) to use “this” in this case.

“this” refers to the instance (with access to non-static methods) and the class name refers to the class overall, and not any instance of it (thus only making possible to use the static methods within it). Both things are pretty different, you can try it yourself with another kind of classes such as strings, try to write “string.” and you will see the IntelliSense showing you the static methods (that don’t depend on an instance), but if you write something like: “new string(“hello”).”, you will see that the IntelliSense shows completely different methods (that are bound to instances, and not to the static class).

If Ben used the “Ball” instead of “this” by the way, the script wouldn’t compile.

Okay I think I now have pretty clear picture here. It’s just that I have to experiment more and get used to it at this level. Many thanks for your time on this! Appreciated !

Privacy & Terms