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:
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?
Answer by $$anonymous$$ · Dec 15, 2016 at 01:11 PM
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."
Answer by BrkTbn · Jul 26, 2016 at 11:23 AM
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.
Answer by kryzodoze · Nov 14, 2016 at 08:40 PM
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.
Answer by $$anonymous$$ · Jul 26, 2016 at 01:24 PM
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.
O$$anonymous$$ I get it now but it's strange, there must be another script which sets the scale back to zero, either on the object you are modifying or elsewhere.
I was afraid that it could be the case that's why I added a Debug.Break() right after the code to see if the scale of the object was at least 1 in the inspector & player. But nothing, the scale stays 0 even right after the code as been executed. What's funny is that in the console it says the scale is 1 while clearly it's not. Could it be a bug from Unity? I came across a problem few months ago were I was stuck like that and it appeared to be a bug. I'm using Unity 5.3.3. I found a way anyway to not use this code but I would like to know what is the problem here =/
Looks like a bug then, maybe you should contact Unity support :)