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.

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

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!

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.

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?