Search Unity

Question Animator somehow has an AnimatorController and doesn't have one at the same time.

Discussion in 'Animation' started by samboknight, Mar 9, 2023.

  1. samboknight

    samboknight

    Joined:
    Aug 31, 2019
    Posts:
    25
    Perhaps someone can explain this baffling conundrum to me. I have this bit of code here. The screenFader variable contains an Animator component which has its controller set in the editor. The assertion and the log message tell me that the controller is not null, and it does, in fact, match what was set in the editor, but as soon as I call Play on the next line, it complains to me that it doesn't have one.

    Does anyone have any idea how and why this is happening? Because I've got nothing.

    Code (CSharp):
    1. private void SetUpFade(Color endColor, float duration, bool fadeOut)
    2.     {
    3.         screenFaderImage.color = endColor;
    4.         var keys = new Keyframe[2];
    5.         if (fadeOut)
    6.         {
    7.             keys[0] = new Keyframe(0, 0);
    8.             keys[1] = new Keyframe(duration, endColor.a);
    9.         }
    10.         else
    11.         {
    12.             keys[0] = new Keyframe(0, endColor.a);
    13.             keys[1] = new Keyframe(duration, 0);
    14.         }
    15.         var curve = new AnimationCurve(keys);
    16.         screenFadeClip.SetCurve("", typeof(Image), "color.a", curve);
    17.         // screenFader.gameObject.SetActive(true);
    18.         Assert.IsNotNull(screenFader.runtimeAnimatorController, "Null animatorController"); // doesn't fire
    19.         Logger.Log(screenFader.runtimeAnimatorController.ToString()); // matches what is set in editor
    20.         screenFader.Play("Fade"); // "Animator does not have an AnimatorController"
    21.     }