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 Hoffa25 · Nov 20, 2014 at 09:39 AM · animationcall

How to call animation from another Script in C#?

I have been stuck on this about 12 hours now:/.

I want to have a countdown and every ten seconds I want a the number to flash with an animation. I have made the animation of the textflash (ther is a animator script attached to the text game object in inspector). In the countdown script in another game object I want to call the animation. I have draged the text animation game object to the animator slot on the script on my countdown game object.

I get the error message "MissingComponentException: There is no 'Animator' attached to the "Countdown" game object, but a script is trying to access it. You probably need to add a Animator to the game object "Countdown". Or your script needs to check if the component is attached before using it." I cant see what Im doing wrong:

 public class Timerstart : MonoBehaviour 
 {
   public Animator animatortest;
 
   public void Start()
   {
     animatortest = GetComponent<Animator>();     // (I know it should be <> and not >>)
   }
 
   public void Callanimation()
   {
     animatortest.Play ("flash");
   }
 }

...also I have no idea why my animation starts when my scene loads?

Comment
Add comment · Show 2
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 Immanuel-Scholz · Nov 20, 2014 at 09:43 AM 0
Share

please edit and reformat your code example. Select the whole code block and just press the "101010" - button above the text input to mark it as "code block"

(code is basically everything with 4 spaces at the start of the line)

BTW: < is an allowed char within a code block. ;)

avatar image Hoffa25 · Nov 20, 2014 at 10:05 AM 0
Share

Ok, I deleted the Start function and I get the same error message:/. In the inspector I can clearly see that my textobject(with the attached animator script) is in the Animator slot on the other game object in inspector. Is this a bug!?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Immanuel-Scholz · Nov 20, 2014 at 09:46 AM

...ther is a animator script attached to the text game object in inspector). In the countdown script in another game object I want to call the animation

You are using GetComponent to retrieve the animator component. This function returns the component on the same gameObject it is called on . If you don't call it on any gameobject, it means "my current GameObject where this script is".

So basically, you have to find the other gameObject first which contains the Animator component and thenn call the GetComponent on that gameObject. (`otherGameObject.GetComponent()`)

However, I assume that you linked the other Animator within the inspector? (Your Animator variable "animatortest" is public, so it is visible in the Editor). If you did that, you already have the correct Animator set up and you just need to delete the Start() function.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Animay play in editor 0 Answers

How to call an Animation? 1 Answer

how to get a animation from other script on colision 1 Answer

animation & position problem 0 Answers

Animation problems 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