- Home /
Destroy GameObject after triggered animation
Hello,
I would like to destroy a GameObject after triggered animation is finished.
In this example I have some tokens on playfield, when the player pick a token this is destroyed after a little animation.
Here the script attached to my token:
public class TokenPick : MonoBehaviour
{
private Animator m_animator;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
m_animator.SetTrigger("Picked");
Destroy(gameObject, m_animator.GetNextAnimatorStateInfo(0).length));
}
}
}
My token have an animator with the "idle" animation in loop.
When the player touch the token I set the trigger "Picked". After that "in theory" the token in destroyed..
In short: I want to use the animator framework without calling PlayAnim,...
The problem is: when I get m_animator.GetNextAnimatorStateInfo it returns always 0.
Where is the error? I'm in a right direction or no?
thanks in advance Lorenzo
Answer by ray2yar · Jul 11, 2020 at 08:01 PM
Is there a reason to not use an animation trigger? If you know the length of the animation you could just hard code that in- not flexible but would work.
Your answer
Follow this Question
Related Questions
Problem with Animator HEEEEEELP 0 Answers
Best Way - Animation - Multiple GameObjects. 0 Answers
Animator problem 0 Answers
changing a game object with another 0 Answers
Non-existent gameObjects appearing. 2 Answers