- Home /
Animator - Do After Playing Animation
Hello everyone,thanks for your time, I'll cut to the point.
-I've created a GameObject. -This gameObject has an Animator attached. -I've created 1 Animation and UNchecked the Loop animation box in the inspector.
I created a function on a script attached to the GO called:
void FinishedPlaying(){
Debug.Log("Animation ended");
}
I want to play this function as soon as the animation ends the first(and only) loop. How can I accomplish this? I tried :
Update(){
if (!_anim.animation.IsPlaying("CodingAnimation")){
Debug.Log("Animation Not Playing");
FinishedPlaying()
}
}
But I see 3 problems to this solution:
1.There is no 'Animation' attached to the GameObject.
2.There's an Animator but not an Animation...Why??
3.If I use this, the function will get called everytime the animation is NOT playing and not just the one time the animation finished playing.
What do you guys recommend I try?
Thank you for your time, help and expertise! Cheers
Answer by ozone · Oct 16, 2014 at 07:01 PM
Perhaps try adding a boolean variable that you set to TRUE when FinishedPlaying() is called and then set back to FALSE when the animation is playing so that FinishedPlaying() isn't called twice in a row. Something like this:
bool alreadyFinished = FALSE;
void FinishedPlaying(){
alreadyFinished = TRUE;
Debug.Log("Animation Ended");
}
void Update(){
if(!_anim.animation.IsPlaying("CodingAnimation") && !alreadyFinished){
Debug.Log("Animation not playing and FinishedPlaying hasn't run yet");
FinishedPlaying();
}
else if(_anim.animation.IsPlaying("CodingAnimation"){
alreadyFinished = FALSE;
Debug.Log("Animation is playing");
}
}
I might implement this, but Im still left with an unanswered question,
Why am I getting "There is no 'Animation' attached to the GameObject." Error, I've got animator attached to the gameobject...
"Animation" and "Animator" are, annoyingly enough, not the same component.
Your answer
Follow this Question
Related Questions
changing a game object with another 0 Answers
Animations playing twice when transitioning 0 Answers
why I have to anim.getComponent in update() function when I had done in Start () function 2 Answers
How to start a instantiate at a specific point of an animation 2 Answers
Animator issue! Please Help! 2 Answers