• 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
0
Question by AnimAlu · Jul 14, 2011 at 03:46 PM · animationguibuttonreference

GUI Button Animation Cue

Is it possible to cue two separate objects animations from one GUI Button? For example, I have the following script attached to my antenna object which triggers an animation that I created in Unity. It works great, but I want to move the camera at the same time. If I attach the same script to the camera, I of course get two buttons on top of one another. Can I somehow reference the camera's animation to play from the script attached to my antenna object? Or is there a better method to do this that I'm not even considering?

Here's the code:

 function OnGUI() 
 {
 if( GUI.RepeatButton( Rect( 25, 25, 130, 30 ), "Deplay Anetnna" ) )
 {
 animation.Play();
 }

}

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
0
Best Answer

Answer by Joshua · Jul 14, 2011 at 04:02 PM

animation.Play(); is actually 'short' for gameObject.animation.Play(), where gameObject is the gameObject your script is currently attached to.

If you want to reference a component of an other GameObject, there are tons of ways you can establish this connection.

You could, at the top of your script have a variable of the Type GameObject or even Animation and drag-and-drop through the inspector:

 var myCamera : GameObject;
 myCamera.animation.Play()
 or
 var myAnimation : Animation;
 myAnimation.Play();

you can also establish this reference dynamically, by using gameObject.Find() for instance. This is often not a good idea however, because it'll do a linear search through all of your gameObjects, which takes relatively long.

If you want to reference your camera however, and it's tagged MainCamera, as it is by default, you're in luck. You can reference it by using Camera.main, so it would look like this:

 Camera.main.animation.Play();

But this of course only works for a MainCamera, the solutions I mentioned above work for every other type of GameObject as well.

Comment
Add comment · Show 1 · 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 AnimAlu · Jul 14, 2011 at 04:15 PM 0
Share

Thanks Joshua, the Camera.main.animation.Play(); method worked perfectly. Appreciate the help!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity 5 GUI system 1 Answer

How to give animation to the on gui button? 1 Answer

Touch GUI texture to toogle animation 0 Answers

Buttons remain in pressed state upon performing home interrupt 0 Answers

Animator.Update High CPU Usage on Unity 5 resulting in bugs on Android 0 Answers


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