- Home /
Animator.Rebind() not working as expected after manually changing GameObject hierarchy
Hi,
At runtime, I am adding a new mesh to a GameObject (which has an Animator component). Once the mesh has been added to the GameObject (and the existing mesh has been removed), I grab the Animator component and call Rebind(). Unfortunately, the player stays in the default T-Pose.
However, if I:
1) Save the following attributes of the existing Animator component:
runtimeAnimatorController, avatar, applyRootMotion, updateMode, cullingMode
2) Destroy the existing Animator component
3) Yield until the end of the frame (so the Destroy operation has actually taken place)
4) Add a new Animator component and assign the attributes I saved in step #1
Then, my model animates as expected. Ideally, I would like to avoid using a coroutine, and simply call Rebind(). Given the above explanation, am I misusing Rebind()? Any recommendations?
Thanks, Brennan.
Having the same issue.
I've created the ability to save the scene (think saved games). I can bring everything back as it was, but the Animator doesn't play. However, if I change any value in the Animator's Inspector (that I just deserialized), then it starts playing the animations.
I hoped Rebind() would fix the problem, and let me do all this through code. It worked, but only if I yield until the end of the frame (per your suggestion). :)
Yes, this unfortunately seems to be the only actual answer at the moment, which sucks, since it's undocumented.
So to be clear, to fix this I had to run Animator.Rebind() in a coroutine (IEnumerator) which first called "yield return new WaitForEndOfFrame()".
I haven't tried it, but I'd guess that calling it on the next frame would also work, as it would appear that a hierarchy change will not get picked up until after the next animation update.
I'm also going to file a bug on this, because calling Rebind() should just account for this automatically, ins$$anonymous$$d of having to force it.
I realise this is an old thread, but did you get a bug logged for this? I've just discovered this exact issue/solution in Unity 5.6 so it's still around. I'll happily vote on a logged bug if there's one present.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
C# Gameobject Is Destroyed before Animation Plays 4 Answers
Problem with timed code-execution (audioplay and object destruction) 1 Answer
Can't remove component. MeshRenderer depends on it 1 Answer
Replace GameObject vs. replacing mesh and material? 2 Answers