- Home /
Problems with Animator when disable/enable gameObject
I have a turret enemy that have a animator that always play a idle animation on it. The idle animation is just some of the top parts of it moving a little. I also have a turret script that is using the turrets 'turret_rotation joint' to turn the top of the turret around so it can look at the player. It works well with the turret turning around and tracking the player. I can select the joint in the editor and if i rotate it myself the turret will rotate from the new angle. So the animation to not clash with my turret script.
If i then disable the turret in the editor and then enable it again things break down. The turret keeps playing it's animation but it no longer rotate. If i select the joint in the editor and try to rotate it no longer works. The turret will remain stuck in the rotation direction it was at when i disabled the turret. If i turn off the animator component the turret will start to rotate again but will not animate anymore. Turning the animator back on will snap it's rotation back to the pre-disable rotation and it will resume animation.
Anyone has had problems like this when deactivate/activate of object seems to mess up the Animator. Seems to be a bug with it's mask or something as it starts to animate one of the joints it should not touch. With an old value also :(.
Add
function OnEnable()
{
Start();
}
or its C# equivalent.
Answer by ProgrammerSi · Oct 25, 2016 at 11:17 AM
I too have this problem. In Unity 5.3 and now in 5.4. Any word from Unity on this?
I managed to successfully work around the problem, i'm pretty certain it's a Unity bug now. The difference between the joint transform before and after it has been disabled/enabled again is that before this it retains the previously set transform. Afterwards it reverts to the transform in the animation pose every time.
It is possible to work around this by flagging that you have disabled the component in OnDisable. And if so handling the rotation differently. Before it has been disabled do what i assume you were doing which is increment the transform by however much you want it to rotate. After it has been disabled/enabled you want to keep a total rotation yourself and always apply this every time.
One thing i have noticed is that this seems to occur when using a mask on the animations. Both the objects i have this problem with use a mask, i believe we have other cases without masks that work normally. Hopefully that will alert some Unity devs.
Still have this bug in 2020 in Unity 2019.3.0f5
Answer by dcdkiller · Aug 20, 2018 at 11:07 AM
You can reset the rotation before deactive, when you active it again ,it works.
I still have this bug on Unity 2020.1.15. What do you mean by "reset rotation"?
Reset it via code in the OnEnable
method. Set it directly to default.