Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DvdRB · Aug 08, 2010 at 09:08 PM · animationguitrigger

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DvdRB · Aug 09, 2010 at 02:47 AM 0
Share

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.

avatar image DvdRB · Aug 09, 2010 at 02:47 AM 0
Share

That didn't work...let me repost as an answer.

avatar image Wolfram · Aug 09, 2010 at 10:49 AM 0
Share

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.

avatar image DvdRB · Aug 11, 2010 at 03:16 AM 0
Share

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!

avatar image Wolfram · Aug 11, 2010 at 10:28 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Adjusting ObjectLabel to activate animations 1 Answer

triggering random animations with gui 1 Answer

Path Trigger Animation 1 Answer

How do i make a text box appear on screen after a chest has been opened? 3 Answers

Animation Trigger Other Events 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges