- Home /
play animation with script
I try to play animation of an object with a script attached on another object.
#pragma strict
var OBJ : GameObject; //Object that have animation, script is attached on another object
var animazione : AnimationClip; //Animation of object
function Start () {
}
function Update () {
}
function On_TouchDown(gesture:Gesture){
OBJ.active = true;
OBJ.animation.Play("animazione");
}
When I play unity say me that not able to found animation.
are you sure you spelled it correctly, or that you attached the animation component and the animation to this component? because other than that, i don`t know what else could be wrong
I try with this:
var OBJ : GameObject;
var animazione : AnimationClip;
var anim : String;
function Start () { anim = '"'+ animazione.name +'"'; }
function Update () { }
function On_TouchDown(gesture:Gesture){
OBJ.SetActiveRecursively(true);
OBJ.animation.Play(anim);
}
Doesn't work. Work only if I write manually the name of animation ("name") in place of variable anim.
Answer by Grady · Jun 17, 2012 at 02:08 PM
Ok, on your first game object with the animation attached to it, attach this script named "playAnimation":
function playAnimation(){
animation.Play();
}
Then on your second game object, attach this:
var animatedObject : GameObject;
function On_TouchDown(gesture:Gesture){
animatedObject.GetComponent(playAnimation).playAnimation();
}
This should work, if it doesn't, then comment back! I'm not at unity right now, so I haven't been able to test it, but it should give you some idea!!!!!
-Grady
Answer by slayer29179 · Jun 17, 2012 at 02:09 PM
I think I may have the answer: what have you called the animation on your character? your script (if I am right) is searching on the game Object OBJ for an animation called animazione try getting rid of the speech marks to link it to that animation variable like so;
OBJ.animation.Play(animazione);
hope this helps!
Answer by aldonaletto · Jun 17, 2012 at 02:15 PM
I suspect you're confusing things: you must pass the animation clip name to OBJ.animation.Play - like this:
OBJ.animation.Play("idle");
This will play the clip called "idle". If you want it to repeat forever, set the wrap mode:
OBJ.animation["idle"].wrapMode = WrapMode.Loop;
OBJ.animation.Play("idle");
Check what's the clip name of the animation you're interested in.
Answer by benfattino · Jun 27, 2012 at 11:37 AM
I try this script without result. Unity say me that can't found animation.
#pragma strict
var OBJ : GameObject;
var SingleTouch : boolean = true;
var DoubleTap : boolean = false;
var Swipe : boolean = false;
var animazione : AnimationClip;
var anim : String;
function Start () {
anim = '"'+ animazione.name +'"';
}
function Update () {
}
function On_TouchStart(gesture:Gesture){
if (SingleTouch){
OBJ.SetActiveRecursively(true);
OBJ.animation.Play(anim);
}
}
function On_DoubleTap(gesture:Gesture){
if (DoubleTap){
OBJ.SetActiveRecursively(true);
OBJ.animation.Play(anim);
}
}
function On_SwipeStart(gesture:Gesture){
if (Swipe){
OBJ.SetActiveRecursively(true);
OBJ.animation.Play(anim);
}
}
Somebody help me?
@benfattino I know this is a really old thread but I've just spotted your mistake.
You should simply write: anim = animazione.name;
If you do '"'+ animazione.name +'"' you're adding quotes to the string literal. That won't work unless the quotes are part of your animation clip name (I suspect they are not?).