LocalScale not working?

Hello,

I know there are a lot of topics about localScale but I assure you, I can’t find an answer to my problem.
So it’s the first time I use localScale and I want to modify the scale of an object (duh). Here’s my code:

public void ResetStarsParameters() {
            Debug.Log(_variables.FolderStar_1.transform.localScale);
            Vector3 scale = _variables.FolderStar_1.transform.localScale;
            scale.Set(1,1,1);
            _variables.FolderStar_1.transform.localScale = scale;
            Debug.Log(_variables.FolderStar_1.transform.localScale);
            Debug.Break();
            //_variables.FolderStar_1.transform.localScale += new Vector3(1, 1, 1);
        }

I tried the same thing with the code on the commented line (it was what I first wrote before looking on internet), even with or without the + sign before the = sign, same result.

In the player, nothing is happening and neither in the inspector:74816-inspector.png

But when I take a look at the console, everything is working perfectly fine:

Am I missing something?

Thank you for your help!

EDIT: By the way I added Debug.Break() to check whether or not the values were changing before I could see it & I forgot to say this code is happening on the last frame of an animation clip as an event. Might it be because an animation controller is attached to the game object this script is attached to?

1 Like

Unity contacted me and said:
"After further investigation this turns out to be by design.

Unity intentionally doesn’t let you change the value of a property you are animating.

What is happening here is you’re changing the local scale value of your object, but since you’re in play mode and the object is animated, it will be overwritten back to the animated value.

As a workaround I can suggest you to either implement it as an actual state machine state, or disable animator and gain back control on its object values."

This code works.

Vector3 scale = transform.localScale;

        scale.Set(4, 4, 4);

        transform.localScale = scale;

There is nothing wrong with your code. Just like you said, if your animation clip contains fixed frames of transforms and it overrides each frame, it might be setting your scale back to (0, 0, 0) after you modify it.

I also just found out that if you have an animation anywhere within that object’s Animator flow that alters the localscale then you won’t be able to alter it.

Ok. That’s true that there is an animator controller on the game object this script is attached to however the code does not modify this game object but another one.

UPDATE:
Ok, so I tried something to try to understand why it’s not working: instead of targeting my object “FolderStar_1” I decided to target some other objects. Here what I discovered:

. On objects with an animator controller attached to them, the code won’t work (it’s a bug and it’s going to be fixed).

. On objects with or without an animation component attached to them it does work.