I've made an object and when the player has pick it up, rise it up and stop it out of screen. But because you can see the shadow of that object, I will destroy it after the animation has played. But how can I do this whit Unity and C#?

Just destroy is just like this:


and start an animation like this:

private Animator _anim;           // global variable
_anim = GetComponent<Animator>(); // in start() methode
_anim.SetTrigger("Picked");       // in other methode

You could do this by checking the current state of the Animator. Add an additional state that your animation will transition to and then check if the animator reaches this state using AnimatorStateInfo.

Or you could just use AnimationEvents

The best way in my opinion is to wrap the functionality and use a callback. This way you could reuse the code as well.

animatorBox.Trigger("Picked", () => {
| improve this answer | |
  • \$\begingroup\$ The animatorBox is that another class then the Animator-class I has used, sir? \$\endgroup\$ – H. Pauwelyn Jan 1 '16 at 8:10
  • 1
    \$\begingroup\$ Yes. This was just to show how you could do it using a wrapper class. The functionality in this class could for example use AnimatorStateInfo to check for completion. \$\endgroup\$ – Mattias Jan 1 '16 at 10:26

I've found also a way to do it:

// other code
Destroy(gameObject, 2f);
| improve this answer | |
  • \$\begingroup\$ Sure. But then you are just asking how to delete a gameObject after x time. \$\endgroup\$ – Mattias Jan 3 '16 at 12:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.