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
0
Question by milliemunchers · Jan 08, 2014 at 10:14 PM · animationjavascriptspriteanimatorreferencing

How can I reference an animator in another script so that it plays the desired animation when a button is clicked?

Hi, I'm currently trying to make a simulation game (kind of like tamagotchi) where you nurture a pet through different interactions. I used a sprite sheet in unity 2d and spliced them to make the different animations needed. (idle, fed, hungry, etc). And I used this code on the Pet's game object

 var anim : Animator;
 
 function Start()
 {
 anim = GetComponent("Animator");
 }
 
 function Update()
 {
 if(Input.GetMouseButton(0) == false)
 {
 anim.SetTrigger("NoRub");
 }
 }
 
 function OnMouseOver()
 {
 if(Input.GetMouseButton(0))
 anim.SetTrigger("Rub");
 }

So that it plays the 'Rub' transition animation when you rub the pet (left mouse + hold) to make him happy and returns to idle state (which is just bobbing up and down) when you stop. Anyway, my question is how can I reference the pet animation in another script, for example a 'feed button'? so it triggers the animation after a click and then reverts back to idle as usual.

I'm not the best at coding and I was wondering if anyone can help me create a variable in the other scripts which is an animator type so that this could work? In one attempt, i tried dragging the animator from the pet into the other script's swatches in the inspector but the feedbutton turned into the pet.

Comment
Add comment · Show 1
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 TimBorquez · Jan 08, 2014 at 10:48 PM 0
Share
 //drag an object with an animation component into this in the inspector
 var objectWithAnim : Animation;
 
 
 function Start () {
 
     objectWithAnim.Play("animation name");
 
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HappyMoo · Jan 08, 2014 at 10:46 PM

You need to trigger NoRub in every frame if you're not rubbing? Why not have a bool "rubbing" instead a rub trigger and stay in the state a long as rubbing=true? Only leave to idle if rubbing=false.

Now you could reference the animator of another object direclty:

 var datAnim = datOtherObject.GetComponent("Animator");

But I think that's bad style and you end up with animator code spread out through your whole project...

Just Reference your Creature from every Button

 var thing:Tamagotchi;

then you can call a public Feed method on them:

 thing.Feed();


and then in the Tamagotchi you have a public Feed function

 public function Feed()
 {
     anim.SetTrigger("Feed");
 }
Comment
Add comment · 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

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I animate a 2D sprite? 1 Answer

Playing sprint animation with vertical axis? 0 Answers

Changing number of frames in uvAnimationTileX 0 Answers

Can't animate Sprite correctly 0 Answers

I'm confused with the animator 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