2
\$\begingroup\$

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:

Destroy(gameobject);

and start an animation like this:

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

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", () => {
    Destroy(gameObject);
});
\$\endgroup\$
  • \$\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
0
\$\begingroup\$

I've found also a way to do it:

// other code
anim.SetTrigger("Picked");
Destroy(gameObject, 2f);
\$\endgroup\$
  • \$\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.