Animation play error. No animation attached.

I’ve been trying to play the animation I created inside of unity. I’ve added the animation to an empty gameobject called “SwingAnimation”(It’s no longer empty). (I created the animation using a cube which I later removed to add a sword.)

The animation is working since its looping every time I test it out.

Picture showing the SwingAnimation GameObject in the Inspector view.

This is the code which I’ve been trying to play the animation with:

swing.js Attached to the Player.

function Update () {

    if (Input.GetButton ("Fire1")) {
        
        GameObject.Find("SwingAnimation").animation.Play("swing");

    }

}

I have also tried attaching the script directly on the gameobject containing the animation directly with:

function Update () {

animation.Play();

}

But all I get is: There is no ‘Animation’ attached to the “SwingAnimation” game object, but a script is trying to access it.

I’m just scratching my head wondering how this can be? There is clearly an animation attached to the gameobject SwingAnimation. What am I doing wrong and how can I fix this?

Thank you in advance for any help you can provide and I apologize if I re-post the question on the matter but I could not find any solution for my problem amongst them. // N0tiC

In order to access the animation, first click the plus button on the bottom left of the animation controller (Parameters) and add a parameter Bool called Fire. Then do the following inside the cube’s Update function (I guess you’ll have to add a new script):
Write var myAnimator: Animator= GetComponent(Animator); This connects the script to the controller. Then write: if (Input etc…)
myAnimator.SetBool(“Fire”,true);

Warning: in order for this to work, you must give the cube an idle animation. So delete the orange block state, add an idle, and make sure it becomes orange. Then add back the original cube animation. You can do all this by dragging by dragging animations into the Animator Controller.

Feel free to ask questions. Also, look up on YouTube a thing called Mecanim and find a Unity tutorial on it to show you the visual tricks.

I hope this is what you meant for me to do:

alt text

Did you notice that it doesn’t have a animation component attached to it? That may just be your problem.