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. Dismiss Notice

Add and play animation

Discussion in 'Scripting' started by tito100, Nov 7, 2016.

  1. tito100

    tito100

    Joined:
    Oct 14, 2016
    Posts:
    22
    Hi all.
    I have this code.

    Code (csharp):
    1.  
    2.     Animation anim;
    3.     void Awake(){
    4.         AnimationClip animClip2 = Resources.Load<AnimationClip>("anims/player/01/ak47/left");
    5.         if (animClip2 == null){
    6.             throw new UnityException("cant find anim clip");
    7.         }
    8.         anim = GetComponent<Animation>();
    9.         if (anim == null){
    10.             throw new UnityException("cant find anim");
    11.         }
    12.         Debug.Log("a-" + animClip2.name);
    13.         anim.AddClip(animClip2, "left");
    14.  
    15.         foreach (AnimationState a in anim){
    16.             Debug.Log("b-" + a.name);
    17.             a.enabled = true;
    18.         }
    19.  
    20.     }
    21.  
    22.     void Start () {
    23.         anim.Play("left");
    24.    }
    25.  
    But its not working. in console I have this:
    Code (csharp):
    1.  
    2. a-left
    3. UnityEngine.Debug:Log(Object)
    4. TestPlayer:Awake() (at Assets/scripts/TestPlayer.cs:12)
    5. -------------
    6. The animation state left could not be played because it couldn't be found!
    7. Please attach an animation clip with the name 'left' or call this function only for existing animations.
    8. UnityEngine.Animation:Play(String)
    9. TestPlayer:Start() (at Assets/scripts/TestPlayer.cs:23)
    10.  
    Any ideas why is not working?
    Im also try set animation clip by public variable and seting it from editor. but its not work too.

    cccc.png

    Tnaks
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Under 'Animations' in the Animation component, try setting Size to 1 and adding the clip you want to change to into that list. Since you're using the legacy Animation component, make sure your AnimationClip is marked as legacy.
     
  3. tito100

    tito100

    Joined:
    Oct 14, 2016
    Posts:
    22
    Thanks. When Im switch unity layout to debug mode, and then set legacy on animation to true, then error was not show.

    But animation is not working. sprite is not changes.

    When Im try add same animation to animation controller then animation was working. but when I not use controller but only Animator. nothings happens.
    Also is not working when I add animation to Animator via editor.
     

    Attached Files:

    • dddd.png
      dddd.png
      File size:
      12.3 KB
      Views:
      850
    Last edited: Nov 7, 2016
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Are you using Animator or Animation? If it's the legacy Animation, do what I said before. If not, it's a completely different process-- clips should not be marked as legacy, and you'll have to add parameters for your animator transitions, which are manipulated by scripts.
     
  5. tito100

    tito100

    Joined:
    Oct 14, 2016
    Posts:
    22
  6. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    I'm pretty unclear about your problem because of the confusion between Animation and Animator when you said this:

    My best guess is that you're saying it works in Mecanim but not with the plain Animation component. Is that correct? Did you add the animation clip to the clips array like I was saying before?

    likethis.png
     
  7. tito100

    tito100

    Joined:
    Oct 14, 2016
    Posts:
    22
    Animation clip in animator component was work before Im checked the legacy in animation clip.
    So now when is checked is not working when I try change Animation to Animator.

    Yes. Im try set animation clip to Animation by editor (and also try disabled script). see screen
    gggg.png

    In script I have now:
    Code (csharp):
    1.  
    2.     Animation anim;
    3.     void Awake(){
    4.         anim = GetComponent<Animation>();
    5.         if (anim == null){
    6.             throw new UnityException("cant find anim");
    7.         }
    8.     }
    9.  
    10.     void Start () {
    11.         bool ok = anim.Play("right");
    12.         Debug.Log("ok:"+(ok?"1":"0"));
    13.     }
    14.  
    in log I have "ok:1"

    Also in animation tab the red line is not moving
     
  8. tito100

    tito100

    Joined:
    Oct 14, 2016
    Posts:
    22
    When Im add position translation to animation, then after play game object was moving by animation. but sprite was not changed.