Hello everyone,
Could anyone tell me why transform.localScale.Set(rate, rate, rate);
doesn’t work and transform.localScale = new Vector3(rate, rate, rate);
works perfectly? If anyone having similar issues using set() try using the other way, I literally wasted 2 hours because i was using localScale.Set()
!! I am using Unity 5.4.1.
Thanks in advance.
transform.localScale
is a property. It has a get and a set method which can do more than just change the Vector3 that represents it.
When you get a property (eg Vector3 scale = transform.localScale;
) it creates a copy of the Vector3 instead of referencing the variable, which means if you do stuff to the new vector it doesn’t effect the transform’s scale.
For a Transform’s localScale property, I think (but I don’t know for certain) that when you set it (eg transform.localScale = new Vector3(x,y,z);
), it also changes all the points and vectors in the GameObject and its children, or something like that, to achieve the effect of being scaled.
transform.localScale.Set(rate, rate, rate);
creates a new a Vector3 value equal to the transform’s localScale value, which means it does not reference the original variable. Then it sets the new Vector3’s x, y, and z components; not the transform’s localScale property.