Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  3. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  6. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Swapping RuntimeAnimatorController during runtime.

Discussion in 'Animation' started by McGravity, Nov 15, 2015.

  1. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Hey there,

    I know that you can set a RuntimeAnimatorController (RAC) during runtime by doing this:

    Code (CSharp):
    1. this.GetComponent<Animator>().runtimeAnimatorController = Instantiate(Resources.Load("Controller")) as RuntimeAnimatorController;
    And it does work fine!

    What doesn't work is swapping the RAC after setting it. It behaves really weird and at first I thought it is a bug (might still be one). So I opened an issue: https://fogbugz.unity3d.com/default.asp?744767_90iduqvcm2ns8umv

    But maybe I'm just doing something wrong or do not take into account some boundary condition. My RACs only contain SateMachineBehaviours (SMB) as I'm using them as behaviour trees for the AI.

    I wrote a very simple script, which loads a RAC in the Start method and substitutes it after 4 seconds with another one. For both cases I use the exact same code as above.

    Afterwards everything seems like the RAC was substituted. The Inspector shows the substituted RAC in the Animator component. The Animator view shows the correct RAC even with runtime/transition visualisation. But through using Debug.Log within the OnStateUpdate method of an SMB, I can see that the old controller is still running. The new one is not. The old controller even tries to access its parameters and gives warning that they are not existent. (Obviously because the new controller doesn't have the same parameters)
     
    Last edited: Nov 18, 2015
  2. Clintonsanto

    Clintonsanto

    Joined:
    Oct 14, 2015
    Posts:
    16
    Code (CSharp):
    1. public RuntimeAnimatorController anim1;
    2. public RuntimeAnimatorController anim2;
    3. //Declare then Old Inspector Drag and Drop method manually my default AnimatorController (PlayerAC) on anim1 (PlayerACbow) on anim2
    4.  
    5. this.GetComponent<Animator>().runtimeAnimatorController = anim2 as RuntimeAnimatorController;
    6.  
    works for my case.. the AC totally changed
     
    Santosh_Nair likes this.
  3. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    You didn't swapp the controller in your code, you just set it. I know that this works. The swapping doesn't work for me.
     
  4. Clintonsanto

    Clintonsanto

    Joined:
    Oct 14, 2015
    Posts:
    16
    i just show the exact line i swap my controller, the actual script are much longer with activated-deactivated gameobjects and all + if (input.GetKeyDown (KeyCode.Q) and else{}

    it actually swapping the controller anim1 was my default AC(PlayerAC) i set manually in the Animator Class on the inspector for using sword stance and it got totally whole different parameters etc

    and after the script run through that code line my AC swapped to anim2 (PlayerACbow) bow stance with another different parameters and all..

    and if i pressed Q again it swapping to my anim1(PlayerAC) again got no error as like you described and works as intended , if i wasn't mistaken of what u've said

    " I can see that the old controller is still running. The new one is not. The old controller even tries to access its parameters and gives warning that they are not existent."
     
    Last edited: Nov 16, 2015
  5. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Okay, thank you very much. I tested it and it makes no difference for me :/ (that way it might be related to the SMBs)
    Which version are you using?
     
    Last edited: Nov 16, 2015
  6. Clintonsanto

    Clintonsanto

    Joined:
    Oct 14, 2015
    Posts:
    16
    hmm :/ .. well that obviously related to your SMB settings if u're using C#, i'm new to unity so i know nothing yet about state machine behavior works as :D.. bcs that code works fine if just simply for swapping RAC on a keypress..

    same as yours 5.2.2f1
     
  7. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    I've found a really ugly workaround. Going to document it here if someone is running into similar issues.

    I guess that the Animator component somehow has serialized data left after swapping the controller. So I'm destroying it and attach a new Animator to the GO before setting the desired runtimeAnimatorController.

    (I also want to swap the controller from within a SMB callback. So DestroyImmediate is not permitted. I avoid this by using Detroy and a coroutine which waits until the Animator is really destroyed before attaching a new one.)
     
  8. x2unity

    x2unity

    Joined:
    Oct 8, 2015
    Posts:
    14
  9. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Hmm, from reading your thread I don't really think it is somehow related. I don't have any problems with the SMB callbacks. They are getting called just fine.

    My problem is, that they are still getting called after swapping the controller. (Still experiencing this in 5.2.3.p1)

    I'd recommend to make a bug report, but that's worthless as a free user :)
    Oh, and it's not the first bug that is "fixed". I regularly step over "fixed" bugs in the issue tracker which I'm still experiencing.
     
  10. x2unity

    x2unity

    Joined:
    Oct 8, 2015
    Posts:
    14
    In my case, after I swap in a new controller, the old controller's SMB gets called, but the SMB on the new controller doesn't.

    And the solution is of to deleting the animator and attaching a new one does work for me. So i think it's likely the bugs are related.

    Thanks
    David
     
  11. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
    Ah well, that does indeed sound exactly like the problem I'm having :)
    Didn't get it that way from the other thread. Keep me up to date if you find out more. I'm also still using that workaround.

    greetings
     
  12. McGravity

    McGravity

    Joined:
    Nov 2, 2013
    Posts:
    43
  13. x2unity

    x2unity

    Joined:
    Oct 8, 2015
    Posts:
    14
    Wow about time. Thanks for the heads up.
     
  14. Aprimez

    Aprimez

    Joined:
    Jul 16, 2017
    Posts:
    1
    Thanks so much!!