The reason transform.position is not updating is because
- Vector3 are structs (value types) instead of a class (reference type)
- transform.position is being accessed using a property instead of a field
To elaborate 1:
How do I know that Vector3 is a struct (value type) and not a class (reference type)? The information is available in the documentation.
Deciding what is a struct or a ref is a design choice and in this case using struct for Vector3 is the correct choice because it is small and always requires the same amount of memory.
However using a value type means that setting a value equal to another value type copies all the data. Vector3s are are value types so
var v1=Vector3.zero;
var v2=v1;
var v2.x=1;
would result in v1= (0, 0, 0) and v2 = (1, 0, 0).
in contrast, Mesh is a class (reference type) so doing the following
var mesh1=new Mesh();
var mesh2=mesh1;
mesh2.subMeshCount=2;
would result in mesh2.subMeshCount=2 and mesh1.subMeshCount=2 even though mesh1.subMeshCount was never assigned to
To elaborate 2:
transform.position is a property instead of a field. You can assume that everything in UnityEngine is a property and not a field. So when you do
transform.position.x = 1;
what you are really doing is
var v2=transform.position;
v2.x=1;
Now combine the two:
transform.position.x = 1;
is the same as
var v2=transform.position;
v2.x=1;
and since Vector3 is a value type this results in transform.position = (0, 0, 0) even though v2 = (1, 0, 0).
To change transform.position you will have to do something like
var v2=transform.position;
v2.x=1;
transform.position=v2.x;
The reason I bring up both that Vector3 are value types and transform.position is a property is because if either Vector3 was a reference type or if transform.position was a field your code would have worked as written. This is a good argument for why a beginner should learn C# instead of javascript. In C# transform.position.x = 1
doesn’t even compile which is good because, as you pointed out, it can’t be used for what you would except. You could use it for something in javascript but again, you’d run into the issue of someone (including yourself) reading your code excepting a different behaviour.