- Home /
Trigger animation from a GUI button?
I am having no problems triggering an animation with a collision, but can't quite work out how to trigger an animation from a GUI button click. Thanks!
Script used:
var mainmenuSkin : GUISkin;
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));
if(GUILayout.Button ("MoveCube")){
animation.Play("GuiCubeMove.anim");
}
GUILayout.EndArea();
}
The script creates the button fine, but I get an error message that reads: "There is no 'Animation' attached to the "GUI" game object, but a script is trying to access it." after adding:
animation.Play("GuiCubeMove.anim");
So, i am evidently only calling the animation, not the object with the animation attached to it, but not sure how to make that link happen. Thanks!
Answer by Wolfram · Aug 08, 2010 at 09:10 PM
Hm? It's just:
function OnGui(){
if(GUI.Button(...))
animation.Play();
}
If that's not what you want, please show us your script and explain what exactly isn't working, otherwise we can only guess what you are doing wrong.
var mainmenuSkin : GUISkin;
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));
if(GUILayout.Button ("$$anonymous$$oveCube")){
} GUILayout.EndArea(); }
Once i add:
animation.Play(GuiCube$$anonymous$$ove.anim);
after the:
if(GUILayout.Button("$$anonymous$$oveCube"))
I receive a "unknown identifier" error. Double checked the animation name to GuiCube$$anonymous$$ove.anim, and tried removing the file extension...totally new to scripting.
The animation needs to be a component of the same GameObject where your GUI script is attached to. If it is not, and it's not possible for you to restructure the scene accordingly, you first need to find the animated GameObject (or assign it in the Inspector using a public var) first, and then start its animation with something like myAnimatedGameObject.animation.Play("GuiCube$$anonymous$$ove"); (you probably don't want to add the file extension .anim, unless the name of your animation also contains that extension when you look at the Animatino component of that object in the Inspector.
OH! Attaching the GUI to my animated object worked perfectly, and brought about yet another "well duh" moment between myself and Unity.
I understand the concept of the public variable, not sure how to execute it though...might have to make that another question for later. THAN$$anonymous$$S!
If you declare a public variable in a script, it will show up in the Inspector when you attach that script to an object. Then you can modify the default value for that variable in the Inspector, without touching the script. This varibal can be anything from a simple int/float number, or even a whole GameObject.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070751im_/https://answers.unity.com/themes/thub/images/avi.jpg)