- Home /
Animation Play On Click
Hey all. I'm trying to script a function in which an animation plays on click of a gameobject. However, instead of naming the animation in the parenthesis, I want to be able to declare it as a variable, so I can use the script for multiple gameobjects so I don't have many scripts for different animations. However, I'm getting an error. Any help?
Error: Assets/Scripting/Click to move.js(7,17): BCE0004: Ambiguous reference 'animation': Click to move.animation, UnityEngine.Component.animation.
var animation : Animation;
function Update() {
if(Input.GetMouseButtonDown(0))
{
animation = AnimationClip;
animation.Play();
}
}
Answer by Sarthak123 · Oct 19, 2014 at 02:40 PM
actually, you have to remove the type "Animation" from your animation for it to work, try this,
var animation : String;
function Update() {
if(Input.GetMouseButtonDown(0))
{
animation.Play(animation);
}
}
this is because the syntax of animation.play is,
animation.play(String); //string will be the name of the animation to be played...
This is what I edited it to. Not sure if this is what you meant. I'm still getting an error: Assets/Scripting/Click to move.js(5,6): BCE0004: Ambiguous reference 'animation': Click to move.animation, UnityEngine.Component.animation.
var animation : String;
function Update() {
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
animation.play(String); //string will be the name of the animation to be played...
}
}
@GraviterX no no... write this in your script... String is a data type, like int is a data type for integers, so is String for String values, i.e. values written inside inverted commas like "$$anonymous$$y Animation", "Walk", etc. are String values, so use only this..
var $$anonymous$$yAnimation : String;
function Update() {
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
animation.Play($$anonymous$$yAnimation);
}
}
Explanation -> we are using $$anonymous$$yAnimation as a string value... so attach this script to your object and then in the inspector, type your animation name inside the box of $$anonymous$$yAnimation, and your script/animation will run..
Hope this works..
Your answer
![](https://koobas.hobune.stream/wayback/20220613165420im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Gameobject does not leave TPose in Playmode 0 Answers
Help with Custom Player Controller 0 Answers
animating, physics, and damage points... ((animating)) 2 Answers
How to run 1 animation, then other 2 Answers