Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to set animation hasExitTime = true through scripting?

Discussion in 'Editor & General Support' started by ClaudiaKrog, Oct 17, 2020.

  1. ClaudiaKrog

    ClaudiaKrog

    Joined:
    Sep 30, 2017
    Posts:
    47
    Hi all,

    I have looked through the Unity documentation, and written a script that randomly picks an animation clip to play, through code. I would also like to make sure these clips transition nicely, with a long exit time.

    However, I cannot get the exitTime to work through code. My code has no errors, but when I play the scene in the editor, the error I get says that " MissingComponentException: There is no 'AnimatorStateTransition' attached to the "xCrowd" game object, but a script is trying to access it. You probably need to add a AnimatorStateTransition to the game object "xCrowd". Or your script needs to check if the component is attached before using it."

    With my code, it seems like I am accessing everything correctly, but I may have missed something:

    AnimatorStateTransition animstateTrans;
    AnimatorTransitionBase animtranBase;
    AnimatorState animState;
    Animator myAnim;
    private AnimationClip[] clips;
    int myNumber = 1;

    private void Awake()
    {
    myAnim = GetComponent<Animator>();
    animState = GetComponent<AnimatorState>();
    animtranBase = GetComponent<AnimatorTransitionBase>();
    animstateTrans = myAnim.GetComponent<AnimatorStateTransition>();
    clips = myAnim.runtimeAnimatorController.animationClips;
    animstateTrans.hasExitTime = true;
    //above line does not run
    }

    If I comment out the last line, everything is fine, but the clips do not transition. I do need to do this through code rather than through the gui. (in the inspector, has exit time IS checked, I don't know if that matters for the code).

    Thanks for any suggestions!
    Claudia