- Home /
How to make a GUI Texture play an Animation of a GameObject?
I've created a GUITexture and would like to use it as a button. Upon clicking the button I want an animation of a GameObject to play. Seems straightforward, but I am stuck.
Here's the JS I have attached to my GUITexture:
function OnMouseDown(){
animation.Play("floorup"); }
Do I have to be more specific in the code? The animation is attached to the GameObject, I assumed there would be no issues.
I would appreciate the help, for I am a noob when it comes to scripting!
Answer by TheVectorHunter · Aug 13, 2012 at 03:36 AM
Your problem is that the GUITexture has the script but it doesn't have the animation. So what you need to do is:
create a variable to store the object to be animated in:
var other : GameObject;
then trigger the animation via the OnMouseDown() function using this:
function OnMouseDown()
{
other.animation.Play();
}
Thank you $$anonymous$$enny!! It works perfectly, and now I understand how to declare a variable. You rock!
Your answer
![](https://koobas.hobune.stream/wayback/20220613075548im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animate GUI Elements 1 Answer
Play a video on clicking on an object 1 Answer
Animator has not been initialized?? 3 Answers
Two Animations With One Button Press 1 Answer