Position bug on changing scale

I downloaded a simple 2d character asset from the assets store
And this prefab consists of different parts with different Z position
[139579-снимок-экрана-2019-06-06-в-135829.png|139579]

However, when I enter to play mode, everything is fine, until I actually change its scale (programmatically): the z position of every object becomes the same: [139580-снимок-экрана-2019-06-06-в-140133.png|139580]

Here is my script:

if(Input.GetKey(KeyCode.D)){
            this.transform.localScale = new Vector2(-1, transform.localScale.y);

I think your problem is that you are setting your scale.z to 0 in that line of code. Meaning that your position z (of childs of that object) is multiplied by 0, with 0 as a result :wink:


transform.localScale is a Vector3. You are giving it a Vector2. A Vector2 is implicitly converted to a Vector3. But as stated in the docs, the z component defaults to 0.


So to fix you problem, I think you want to do this instead:

this.transform.localScale = new Vector3(-1, transform.localScale.y, 1);