Why does Unity want you to use Vector2 to change a game object's position, instead of just using transform.position.x?

Because Vector2's are value types which means they are passed as copies. By referring to its x field you are assigning a float to a copy that will simply be deallocated when it leaves the stack-frame.

This

obj.transform.position.x = newX;

Returns first a copy of the position Vector2, and then you acces the x-field of that copy.

while this

obj.transform.position = newVector;

assigns a copy of your new vector directly to the Transform class vector.

To understand it better it might be useful to think about it like this:

obj.transform.position.x = newX; 

// Let's break it down what this actually means in code:
Vector2 myCopy = obj.transform.position;
myCopy.x = newX;

// Method exits and myCopy is deallocated, your transform is unaffected

/r/gamedev Thread Parent