Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Unity 2018.3 is now released.
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Many unreferenced AnimatorOverrideController objects

Discussion in 'Animation' started by Jinxology, Jul 8, 2018.

  1. Jinxology

    Jinxology

    Joined:
    Jul 13, 2013
    Posts:
    54
    I've been hunting a memory leak in my game, and I've found a potential culprit! In the profiler, I've noticed a number of unreferenced AnimatorOverrideController objects, and the number keeps growing. The offending code is:

    Code (CSharp):
    1.  
    2. AnimationClip clip = Resources.Load("Bodyparts/" + newClipName) as AnimationClip;
    3. Animator anim = transform.Find("body").GetComponent<Animator>();
    4. AnimatorOverrideController overrideController = new AnimatorOverrideController(anim.runtimeAnimatorController);
    5. overrideController[oldClipName] = clip;
    6. anim.runtimeAnimatorController = overrideController;
    7.  
    As you can see from the profiler screenshot below, the ref count on several is blank (zero?). I'm guessing that the original anim.runtimeAnimatorController is getting lost and left behind. However, when I tried to Destroy() the original controller, it told me "Destroying assets is not permitted to avoid data loss.". How can I prevent these unreferenced objects? Halp!

     
  2. Jinxology

    Jinxology

    Joined:
    Jul 13, 2013
    Posts:
    54
    Figured it out. For some reason, when you Destroy() the object that you've set runtimeAnimatorController on, it doesn't destroy the new override controller you replaced it with. You need to manually destroy that. I used the code:

    Code (CSharp):
    1.      private void OnDestroy()
    2.      {
    3.          Animator anim = transform.Find("body").GetComponent<Animator>();
    4.          if (anim.runtimeAnimatorController.GetType() == typeof(AnimatorOverrideController)) {
    5.              Destroy(anim.runtimeAnimatorController);
    6.          }
    7.      }
    Hope that helps someone else having this issue.