button trigger animation

I aim to use Gui button to trigger animation ,I achieve it duing testing in a scene (less object ),However when I import it to my project ,it fails .can anybody help me ?
Thank you in advance ! belove is my code .

var mainmenuSkin : GUISkin;
var ButterflyGameObject : GameObject;
var beep : AudioClip;
var menuSkin : GUISkin;
var areaWidth : float;
var areaHeight : float;

function OnGUI ()
{
    GUI.skin = mainmenuSkin;

    var ScreenX : int = 10;
    var ScreenY : int = 10;
    var areaWidth : int = 100;
    var areaHeight : int = 100; 

GUILayout.BeginArea (Rect (ScreenX, ScreenY, areaWidth, areaHeight));
animation.Stop();
//GetComponent(ButtonTriggerAnimationScript).enabled = false;
//find.ButterflyGameObject;

    if(GUILayout.Button ("Butter"))
{
      ButterflyGameObject.animation.Play();
	  //animation.CrossFade("name of the animation");

}

GUILayout.EndArea(); 

}

2 Answers

2

Make sure your animation file is linked to your object in the inspector window under the animation component and that the animation component isn’t unchecked. your animation should be listed in the ‘Animation’ field and listed as one of the animations in the ‘Animations’ array.

That’s the only think I notice could account for it. Script looks fine and seems like you’re attempting to play the default animation so I would suggest making sure the animation file is linked to the ButterflyGameObject via Inspector → Animation Component

Sorry if this is no help.

I have attached the Animation and Script to the ButterflyGameObject and checked the Animation box as well . Thank your very much your effort !