Add and play animation

Hi all.
I have this code.

    Animation anim;
    void Awake(){
        AnimationClip animClip2 = Resources.Load<AnimationClip>("anims/player/01/ak47/left");
        if (animClip2 == null){
            throw new UnityException("cant find anim clip");
        }
        anim = GetComponent<Animation>();
        if (anim == null){
            throw new UnityException("cant find anim");
        }
        Debug.Log("a-" + animClip2.name);
        anim.AddClip(animClip2, "left");

        foreach (AnimationState a in anim){
            Debug.Log("b-" + a.name);
            a.enabled = true;
        }

    }

    void Start () {
        anim.Play("left");
   }

But its not working. in console I have this:

a-left
UnityEngine.Debug:Log(Object)
TestPlayer:Awake() (at Assets/scripts/TestPlayer.cs:12)
-------------
The animation state left could not be played because it couldn't be found!
Please attach an animation clip with the name 'left' or call this function only for existing animations.
UnityEngine.Animation:Play(String)
TestPlayer:Start() (at Assets/scripts/TestPlayer.cs:23)

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.

Tnaks

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.

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.

2844736--207720--dddd.png

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.

Animation component.
see Animation not playing - Unity Engine - Unity Discussions

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?

2844801--207726--likethis.png

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

In script I have now:

    Animation anim;
    void Awake(){
        anim = GetComponent<Animation>();
        if (anim == null){
            throw new UnityException("cant find anim");
        }
    }

    void Start () {
        bool ok = anim.Play("right");
        Debug.Log("ok:"+(ok?"1":"0"));
    }

in log I have “ok:1”

Also in animation tab the red line is not moving

When Im add position translation to animation, then after play game object was moving by animation. but sprite was not changed.