Hello,
I’m trying to change the direction of a character through this:
transform.localScale.x *= -1;
The error that I get is : “cannot modify the return value of transform.localScale because it’s not a variable”
it works with this code:
Vector3 myScale = transform.localScale;
myScale.x *= -1;
transform.localScale = myScale;
From my point of beginner’s view, these 2 code sequences make the same thing.
Please kindly tell me why I can’t change the code, using the first version.
Thank you !
The reason you cannot do this is because transform.localScale is a property, not a direct link to the variable itself, so its sort of like the same thing as calling a function like “GetLocalScale()” where the return value is the localScale. Fortunately properties can have get and set parts to them, however when used with structs, you have to set the whole value, you cannot just set a part of it. So the simplest way I’ve found to do what you’re trying here is just to use a temporary Vector3 like this:
Vector3 lTemp = transform.localScale;
lTemp.x *= -1;
transform.localScale = lTemp;
Hope this helps.