Losing Animator State

Sometimes you need to create a checkpoint system. To do this, the state of non-static and/or moving objects needs to be saved, as some of these might get destroyed later in the game.

One particularly easy way to do this is creating duplicates of those GameObjects that need to be saved and disabling them. Then, when loading the checkpoint, the original objects (if they still exist) are deleted and the duplicates are activated.

However, If a duplicated object had an Animator component, the activated copy is not identical to the original. This may cause some trivial errors such as incorrect model pose, but may be more troublesome e.g. if physics is involved (animated doors that are closed instead of opened etc.).

It would have been really, REALLY awesome if the Animator could simply save its exact state. As others have already stated, this is consistent with other Unity objects

1 Like

Another hero meet this problem :slight_smile:
Found keepAnimatorControllerStateOnDisable only by code completion. Later found it in this thread :expressionless: first question - why this is still not editor property?

Unity animations are really hard to play with: simple task - menu animation grown to some monstruous code-ui-stuff-mix. Thats why:

  • Conditional transitions from init state is not working (hi to hack with extra state)
  • States w/o motions are hard (or impossible) to setup as freeze-frame for given offset
  • Material animations are not supported (yes, they can be shared, yes, it’s possible to write property-setter-wrapper, but…)
  • Animation state/params are not preserved on enable/disable (w/o help of this thread :slight_smile:

Don’t want to mention dev team, but i think it’s time to completely rewrite Unity Animations for actual dev needs and actual Unity state (URP, materials, new ShaderGraph etc) :expressionless:

They are making a new animation system called Kinematica.

But I have faith in their ability to screw it up again just like the last 3 attempts and totally miss the basic needs of an animation system.

Kinematica is character animation system w/o state graph (per description, mentioned auto posing and so on). Not sure it’ll be suitable for simple animations like menu transitions.

Anyway i solved my problems with UI with a help of hacks and garbage code :slight_smile: Will generalize it after finalizing of UI concept.
Btw, thats why i don’t believe in bolt - it’s easier to write code from start comparing to write workarounds later for system built visually.

private void Awake
{
GetComponent().keepAnimatorControllerStateOnDisable = true;
}

This keeps default values now.

9 Likes

Thanks! :smile:

2 Likes

Was doing optimization by disabling animator for doors and other “fixed” state items… and found out they were losing states. Glad that flag exists.

But honestly, there should be an easy way to “pause” an animator so it doesn’t take any CPU cycles.

1 Like

…wtf Unity!

7 Likes

You Rock

2 Likes

Also wtf def using this from now on

I’ve written a fairly basic state retention script when you do a .Rebind(), you can find it here:
Animator rebind function that also maintains some state (github.com)

You can call it with yourAnimatorHere.RebindAndRetainState();

Any improvements would be welcome, but I’m hoping someone might find that useful/a good starting point

1 Like

I second this

Thanks wanted a script to easily save an animator data state to apply it again on a new instance. I did a fork there https://gist.github.com/builder-main/40b429fce73b60301c73977614389948

1 Like

What a cringe, i have animator with many states in it, each state bring something new in object state,
so when i set keepAnimatorControllerStateOnDisable and disable object i wanna see same look when i enable it back, but actualy animator applyes default values from animator initialization + values of last state and no care about animator evolution befoure disabling, it’s trash behavior and keepAnimatorControllerStateOnDisable not make what must.