Is it possible to change localScale.x in Unity without creating a new Vector3?
1 Answer
Yes, simply write: localScale.x = 100.0
or whatever you'd like.
This doesn't work when the localScale
you're referring to (which is not clear) is a property of some instance, because C# properties are just syntactic sugar for function invocations and you'd thus be changing a member of a local temporary, not the actual backing Vector3
. In those cases you must replace the entire Vector3
, as in foo.localScale = new Vector3(x, y, z);
.
This may be a minor inconvenience in terms of typing, but since Vector3
is a value type there is no appreciable performance overhead (you're not creating garbage on the heap, for example, as you would if Vector3
was a reference type).
transform.localScale = new Vector3(changedXScale, transform.localScale.y, transform.localScale.z);
that you're trying to solve? This can be expressed in several different ways, depending on your objective. \$\endgroup\$