How to reset animator controller ?

Hello.
I hay a question regarding animations,animation controllers,animators
Is there a way how to “restart” animatorcontroller ?


For example :

If i have 2 guns with their own animations and animator controllers and switch betwean guns.
One gun gets disabled and the other gun gets activated the animations gets stuck in weird position/rotation.


So is there a way how to restart the animator so animations would play normally without weird po sitions/rotations.


I have tried

  • creating a boolean and forcing the animator to play the 1st animaton ( taking gun out )
  • disabling the animator and reactivating it
  • destroying the animator component and creating it again ( didn’t work probably wrong setup)
  • I have also tried the Animator.Rebound() -don’t know how but it didn’t work
    Anybody knows how to fix this ?
  • I have also heard about deleting animation defaults or something like that ( didn’t tried - don’t know how )

I am using Unity 2017.3

This works for me, at least for a simple case. It returns the AnimatorController to the Entry state:

Animator anim = gameObject.GetComponentInChildren<Animator>();
anim.Rebind();
anim.Update(0f);

Probably the best way it’s just start the idle animation after the switching a gun.
For example:

anim_gun.Play("idle", -1, 0f);
  • ‘idle’ is your animation which should play
  • ‘-1’ is the layer, -1 means default (first)
  • ‘0f’ is start time of the animation

This isn’t the solution but there REALLY should be something as simple as Unity having some in-built function that when you use gameObject.SetActive(false); you can also select to save the object’s states or not. And then when you reactivate the object it will either be reset completely or be in exactly the same states it was when you deactivated it. It really should be THAT simple, something like gameObject.SetActive(false, true); with the first argument saying if it’s going to be activated or deactivated and the second saying whether you want to save all its states when deactivated or not. So if I set it to false, false then when I reactivate my gun every single thing about it will be back to the default state, on all its components and children and their components too.
I mean, maybe I’m simplifying it, but you get the point.

I think need SetActive(false) for Animator
When want to Play ,First SetActive(true) , Second
anim.Update(0f);
anim.Play(“idle”, -1, 0f);