How to initialize Animator?

Hi to all!

This is probably a which-script-runs-before-which-script typpa timing problem.

  1. I have a UI gameobject, the scale is set to Vector.One
  2. Animator with Animation that scales gameobject from Vector.Zero to Vector.One is attached
  3. We have a script in the screen that Instantiate this gameobject (prefab)
  4. the object is instantiated with scale == Vector.One and animation kick in next frame and start scaling the object from Vector.Zero to Vector.One.

On initialize: Vector.One
Next frame: Vector.Zero
Next frame: Vector.0.3
Next frame: Vector.0.6
Next frame: Vector.One

Something like this.

I think this is the exact problem :
Animated character appears in T Pose in its first frame - Questions & Answers - Unity Discussions
I had the exact same problem in other projects too.
Since I can’t get the values from Animation curves at frame 0 at runtime, this is how I’ve been patching / initializing it on Awake.

anim.Update( 0f );
anim.enabled = false;

Everything works fine till last week, my colleague received a new prefab from designer that uses different Animator & Animation and we start getting…

Assertion failed: Assertion failed on expression: ā€˜m_DidAwake’
UnityEngine.AnimationClip:Update( float deltaTime )

I’m pretty sure this is a timing issue cause there’s no ā€œT-pose situationā€ and it plays fine afterwards if I removed the anim.Update(0) call.

Then I start digging, observing and comparing the differences between the OK and not-OK animators’ values, trying to find a hidden flag or something so that I can decide whether to call Update(0) or not on Awake.
I had no luck, values seem identical.

I then tried, anim.runtimeAnimatorController.animationClips[0].SampleAnimation( gameObject, 0 );
It gives me the same m_DidAwake assertion.

anim.Play( anim.runtimeAnimatorController.animationClips[0].name, 0, 0 );
No m_DidAwake assertion, but gives you the t-pose.

I’m outta options,
The only patch I can think of is…

  1. provide a bool flag in the animator.
  2. white a editor script to store frame 0 values from Animation curves when designer hit ā€œApplyā€ and use those values for initializing later at runtime.

Method 1… too manual. Method 2… extra step + many prefab needs to be re-apply and there must be a easier way and I think Unity should provide a fix not us fumbling around.

Regards,

edit:
btw, I’m running Unity 2017.2.0p2

1 Like

I have the same problem
But I found Duplicate new GameObject to solve the problem
After comparing, I found a new GameObject, a script that calls Animator.Update, and the serialization position changed
May therefore change script internal calling order

I had the exact same issue.
To resolve it you need to delay the script that contains
anim.Update( 0f ); in the Script Execution Order

I ran into a similar problem while developing my Animancer plugin, but I only got it while using the Playables API, not with regular animators.

Regardless, I haven’t seen the problem since upgrading to Unity 2018 so if it’s related to your problem upgrading will probably fix it.