- Home /
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?
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. ;)
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!?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613172008im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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