Basic Logic makes absolutely no sense [JS]
Please help, I've got this simple test code, and it's producing the incorrect result. It's been bothering me for the past hour, and I still don't get what's going on. The code loops in an update function:
print ("Before: " + numA.transform.position.x);
numA.transform.position.x = 1;
print ("After: " + numA.transform.position.x);
The output result it produces:
numA.transform.position.x is not referenced anywhere else in any other scripts. Any clues on as to why this is happening?
Answer by DiegoSLTS · Oct 17, 2015 at 02:23 PM
Get the position into a variable, update the x value, set the position again. That's how it's done in C#, I thought that UnityScript did this automatically when you tried to update one component of the position, but I may be wrong.
This happens because Transform.position is a property and to set the property you have to set a new value on it. When you write transform.position it returns a NEW vector3, so when you change the x component of that vector the actual position (a private Vector3) is not being modified.
Answer by maccabbe · Oct 17, 2015 at 03:41 PM
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.
http://docs.unity3d.com/ScriptReference/Vector3.html
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.
https://msdn.microsoft.com/en-us/library/t63sy5hs.aspx
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;
https://msdn.microsoft.com/en-us/library/9d65as2e(v=vs.90).aspx
https://msdn.microsoft.com/en-us/library/ms173118.aspx
https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx
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.
Your answer
Follow this Question
Related Questions
HDRP Wierd lighting 0 Answers
My UNITY is not loading 0 Answers
Script function being applied to everything it's attached to logic error 2 Answers
Plane and Terrain Bug? 0 Answers