Make animation play on button press?

I have a script that I want to Make an animation play when you click the left mouse button. Here’s the script…

var swing : Animation;
var sound : AudioClip;
function Update () {
if(Input.GetButtonDown("Fire1"))
{
    AudioSource.PlayClipAtPoint(sound, transform.position);
    GetComponent.<Animation>().Play(swing);
}
}

But, I get this error…

Assets/Swing.js(8,36): BCE0023: No appropriate version of ‘UnityEngine.Animation.Play’ for the argument list ‘(UnityEngine.Animation)’ was found.

How can I fix this?

You’re trying to pass the wrong type of argument to the Animation.play() function. You can only pass the name of the animation as a string value. You’re trying to pass the animation itself. You can easily fix this by just adding “.name” to the end of the animation variable to pass the name instead like so:

    var swing : Animation;
    var sound : AudioClip;
    function Update () {
    if(Input.GetButtonDown("Fire1"))
    {
        AudioSource.PlayClipAtPoint(sound, transform.position);
        GetComponent.<Animation>().Play(swing.name);
    }
    }

I didn’t get that error anymore, but I tried the script, and it said it couldn’t find my animation. I have the animation attached to the GameObject “pipe”. It let me drag pipe into the animation variable. When I clicked the button, it says that the animation could not be found. Here’s the error…

The animation state pipe could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘pipe’ or call this function only for existing animations.
UnityEngine.Animation:Play(String)
Swing:Update() (at Assets/Swing.js:9)

Why is this?

Oops. I forgot that you need to also change “Animation” to “AnimationClip”. This will let you get the name of the actual animation clip you’re trying to play, rather than just the animation component’s name.

Also, make sure that the animation clip you want to play is already assigned to the “Animations” field on the animation component. If not, you’ll get another error that the animation couldn’t be found.

var swing : AnimationClip;
var sound : AudioClip;
function Update () {
    if(Input.GetButtonDown("Fire1"))
    {
        AudioSource.PlayClipAtPoint(sound, transform.position);
        GetComponent.<Animation>().Play(swing.name);
    }
}

An even simpler way to do this would be to just assign the animation clip you want to play to the “Animation” field on the animation component and simply call play without passing it anything. Like this:

var sound : AudioClip;
function Update () {
  if(Input.GetButtonDown("Fire1"))
  {
  AudioSource.PlayClipAtPoint(sound, transform.position);
  GetComponent.<Animation>().Play();
  }
}

This will play the default or currently assigned clip on the animation component. However, you will probably want to uncheck “Play Automatically” on the animation component so it doesn’t play before clicking the button.

I still got the same error…

The animation state pipe could not be played because it couldn’t be found!
Please attach an animation clip with the name ‘pipe’ or call this function only for existing animations.
UnityEngine.Animation:play(String)
Swing:Update() (at Assets/Swing.js:9)

It automatically changes ‘colon P’ to :p.

Actually, I went to assign the animation to the variable, and I went to the scene tab. It said there were no animations in the scene. Why is this? I have both an animator and an animation clip attached to the object.

“animation” legacy

“animator” “animation clip” more recent system

This error means Unity can’t find any animation clip named “pipe” on the animation component. If “pipe” is the correct name of clip you’re trying to play, then you must not have it assigned to the animation component. Add the clip named “pipe” to the animation component as shown below:

2612337--183185--unity_animation.png

If that’s the only clip on the animation component then you can simply use GetComponent.().Play(); (as in my last example) to play it without needing to provide an animation clip name.

Only game objects in the current scene show up under the “Scene” tab. You need to select the “Assets” tab to find animations and other content that is not part of the current scene. The Animator component is also not needed if all you want to do is play this single animation clip. They are two completely different systems.

I know I’m probably getting annoying at this point, but it still won’t work. Here’s my code now…

var sound : AudioClip;

function Update () {

    if(Input.GetButtonDown("Fire1"))
    {
        AudioSource.PlayClipAtPoint(sound, transform.position);
        GetComponent.<Animation>().Play();
    }
    }

I don’t get an error or anything any more though. Here’s how I have the animation set up in my scene ( I renamed it swing).

2613741--183342--unity issue.png

I’m guessing the animation just isn’t playing for you now? Is the sound at least playing? This might be due to mixing new and legacy animations together.

I think you’re confused with how the Animation and Animator components work. The Animator component is actually a replacement for the Animation component. The Animation component only still exist for backwards-compatibility. Although It’s actually easier to use the old legacy animation component here for a single animation like you want, Unity doesn’t recommend using it any more and may remove it in a future release.

Using both an Animation and Animator component on the same object is pointless because they don’t actually work together. Neither component needs or uses the other. So you should delete one of them. Since you’re trying to use the animation component through code you should probably delete the Animator, since it’s not used or needed in the code. However, I see you’ve also created an animator controller. Are you actually using that for any animations? If not, then you should delete it. However, if you are using it for something you’ll actually want to delete the animation component and access the Animator through code instead, like this:

// This script plays an animation using the ANIMATOR component.
// The animator controller must have a trigger named "DoPunch" with a clip
// that plays when this trigger is set in order to actually work!

#pragma strict

var sound : AudioClip;

private var animator : Animator;


function Awake ()
{
    // Get and store animator reference at start.
    // Optional optimization since GetComponent is slow.
    animator = GetComponent.<Animator>();
}


function Update ()
{
    if(Input.GetButtonDown("Fire1"))
    {
        // Play sound.
        AudioSource.PlayClipAtPoint(sound, transform.position);

        // Set animator trigger "DoPunch".
        // Animator plays a clip when it gets the trigger.
        animator.SetTrigger("DoPunch");
    }
}

I’ve created a simply example project to hopefully help you understand the systems better. It implements both a Animation and Animator example. However, I’d recommend just going with the Animator for all your needs and forget that the Animation component even exist. This is not just because it might be removed in the future, but also because there are compatibility issues when mixing them. For example, I had to create a copy of the model for this example and mark it’s animations as “Legacy” in order to get it working with the old Animation component. I believe you can use legacy animations with the Animator component, but you can’t use newer animations in the old Animation component. Confused yet? :stuck_out_tongue:

Example Project (for Unity 5.3.4x)

There is also a good tutorial here if you want to learn more about using an Animator for this.

what in the script is the name of the animation?

It actully does not matter.

The above code is all UnityScript (aka “JavaScript”) and is no longer supported by Unity.

If you copied ANY script from above, just delete it, it will not be useful to you. NONE of it will work anymore.

This is why the forum rules ask you NOT to necro-post.

Now go look at a recent tutorial. This is simple-simple-simple stuff! Buttons are covered millions of times, and starting animations are also covered.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Finally, when you have errors, don’t post here… just go fix your errors! Here’s how:

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

See above. Closing.