Property in animation doesn't take default value when object is disabled / enabled again

Hello !

I’ve been working on a project that includes a lot of UI elements animations.
Unity version when starting the project : 2021.3.17f1
I had to upgrade to 2021.3.32f1 and now, a lot of my animations are broken.

When I animate a UI element, I have several animations, typically : opening, closing (that results in disabling), and some in between that do various stuff.

Before the Unity upgrade, when a Property didn’t have any Key in a Clip, it would take the default values (ie that value when the animator is disabled). Let’s call it behavior 1.

But now with the newer Unity version, the first time the object is enabled, everything runs fine, but when the object is disabled (typically after a closing animation), and enabled again, the properties that don’t have specific keys do not take default values anymore ! Instead, I believe they take values that were set by other clips at some point. Let’s call it behavior 2.
This is quite annoying because it means that everytime I add a key for a property in a given clip, I need to make sure that ALL the other clips for the same object include also a key, even if it’s just to set it to the default value.

I did a lot of tests with many Unity versions.
The behavior 1 occurred on Unity version 2021.3.16 (included) to 2021.3.19 (included).
Then on the 2021.3.20 I could read in the patch notes :

I’m pretty confident this was the “fix” that made this change of behavior.

But now I’m confused : behavior 2 really looks like there’s a bug to me : Why would it be considered “normal” that an animation works in a certain way when it’s first enabled, but then works differently when it’s disabled and enabled again after ?

I’ve tried to play around with the checkboxes “Write defaults” when selecting a clip but with no luck : I just don’t understand how it’s supposed to work, or if there is any chance it will get me to the precious behavior 1 I was looking for.

This has been a big concern for me lately because not only does it mean I need to rework all my existing animations by adding “manually” all the “default-value-keys”, and this means that my future workflow will also be much heavier. Also it’s very hard to debug because you need to manually trigger all animations configuration on Play mode, with enabling / disabling objects, and it’s impossible to anticipate issues when not in Play mode.

Any help / guidance on this topic would be extremely appreciated. Sorry for the long text, tried to be as clear as possible but let me know if I can illustrate my problem further.

Thanks !

Hello.
Try to set keepAnimatorControllerStateOnDisable to true via script on Awake.
May be it will fix your problem.

Also in onEnable you can call animor.play(“SomeYourDefaultState”) if it is needed to always start from that default state. Right after that You can also call animator.Update(0) to immediately update the state of animator.

Hi, thanks for your reply!

I tested the first step on a test project and it seems to have done the trick !

Thanks again :slight_smile:

1 Like