Search Unity

Playing an animation from code

Discussion in 'Animation' started by jbjbjbborisb, Sep 26, 2015.

  1. jbjbjbborisb

    jbjbjbborisb

    Joined:
    Jun 8, 2014
    Posts:
    54
    I'm trying to play an animation from code using the following lines:
    Code (CSharp):
    1. Animator mAnim = this.GetComponent<Animator> ();
    2. ...
    3. mAnim.Play("Ui_In", 0);
    But instead of playing the animation, the following warning is issued:
    A screenshot of the states in the animation controller is attached.
    What could be the cause of this problem? Thanks!
     

    Attached Files:

  2. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
  3. jbjbjbborisb

    jbjbjbborisb

    Joined:
    Jun 8, 2014
    Posts:
    54
    So there is no way to do this without transitions and triggers?
    Seems somewhat overcomplicated...
     
  4. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    What I meant is... try this:
    Code (CSharp):
    1.     Animator anim;
    2.     int yourAwesomeAnimation = Animator.StringToHash("Ui_In");
    3.  
    4.  
    5.     void Start ()
    6.     {
    7.         anim = GetComponent<Animator>();
    8.         PlayYourAwesomeAnimation();
    9.     }
    10.  
    11.     void PlayYourAwesomeAnimation()
    12.     {
    13.        anim.Play(YourAwesomeAnimation);
    14.     }