Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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.     }