[SOLVED] Help with Animating Via Script

SOLUTION!

Okay I know there are countless posts on this but I’m honestly wondering if my install of Unity is corrupted or something. So I do not know how to play animation via a script.

I have Animation component on the object with the script that has this line of code:

this.GetComponent<Animation>().Play ();

That does not return any errors nor does it play any animation. I’ve also tried referencing the animation name with “…Play (“cannon_recoil”);” and that returns an error saying animation was not found. And yes, I do have the animation as the default animation for the Animation component and spelled it right in the code.

I am tearing my hair out since everything I’ve found says this is how you do this.

Any help would be GREATLY appreciated!!

EDIT: I’ve tested and logged default state name to console in Update. It recognizes the default state properly except on first frame that game is loaded, but every frame after that it’s fine. Still doesn’t explain why the clip “doesn’t exist” when I reference it to play it since I reference it 5 seconds after level starts.

EDIT II: I log default clip name to console in Update and if I try to play the default clip in Update, it says animation not found. So animation both exists and does not exist in same frame every frame. This is why I’m confused.

EDIT III: This is the code I’m testing with:

    void Update ()
    {
        Debug.Log (this.GetComponent<Animation>().clip.name);

        this.GetComponent<Animation>().Play (this.GetComponent<Animation>().clip.name);
    }

This is the result I get:

As far as I know, that’s the way to go about it. I’m not terribly familiar with the animation component, so I can’t recall any pitfalls or things overlooked. I do know that that line of code has worked for a number of people on the forums :slight_smile: (and/or the one with the string name of the animation).

Have you ever tried using Mecanim? It’s a little more to setup, but “newer”. While you’re trying to work this out, you could give it a shot and see if you get it working.

Sorry, wish I could be more help, but hope you get it sorted out :slight_smile:

I know it works for most which is why this is particularly frustrating since I have no idea why it’s not working for me. This has never worked for me actually, I’ve tried in the past and never figured it out so I just avoided scripting animations. (is a stretch but) could it be a hardware bug? Like my PC has old hardware so that’s the only explanation I have for this to never work. I even followed step-by-step video guide and it didn’t work for me. I must be special :frowning:

I’ll look into Mecanim thanks!

The example in the manual calls the clip name:

I know I’ve had trouble with it in the past but that was a long time ago. Mecanim is a lot nicer once you get used to it. Much easier to debug. I think if you just drag a clip into the project window it will set up an animator for you. Since it will set it up for the default, you can just get the animator and use play(). I believe.

SOLVED!

So I don’t know why the animation component wasn’t working for me, but I changed to animator and I simply change the anim state.