Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    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:
    45
    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
     
unityunity