Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Destroy object and play animation AT THE SAME TIME!

Discussion in '2D' started by harveyshideout, Jun 24, 2015.

  1. harveyshideout

    harveyshideout

    Joined:
    Jan 12, 2014
    Posts:
    33
    if anyone can help would be great, its a typical collect an egg and when you collect theres a burst of stars...1 month into coding might take me 20 more years,

    if anyone can ever help me with c# code id be happy to supply some art as a ty

    http://harveyshideout.blogspot.ca/
     
  2. harveyshideout

    harveyshideout

    Joined:
    Jan 12, 2014
    Posts:
    33
    This is what I have, however the animation plays because I dragged the game object as a child of the the collectable. I just want it to play when its collected!!

    usingUnityEngine;
    usingSystem.Collections;

    publicclassEggBehavior : MonoBehaviour {

    voidOnTriggerEnter2D(Collider2Dcollider)
    {
    Destroy(this.gameObject);
    animation.Play("CollectEggEffect");

    }

    }
     
  3. Outwizard

    Outwizard

    Joined:
    Jun 25, 2015
    Posts:
    22
    Not quite sure what you mean. So the animation does play even though you destroy the gameobject? In my understanding, if you destroy the gameobject, the animation tied to the object will get destroyed as well.

    What I usually do, is disable the sprite for the "Egg" when it is collected, then play the animation, then when animation is played I can Destroy the Egg gameobject.

    Also, if you don't want the animation to play at start, create an idle state and active state for the animation.

    Bear Boom
    iOS: https://itunes.apple.com/us/app/id985992564?mt=8
    Android: https://play.google.com/store/apps/details?id=com.Airbear.Airbear
     
    Last edited: Jun 26, 2015
  4. harveyshideout

    harveyshideout

    Joined:
    Jan 12, 2014
    Posts:
    33
    hey thx for your reply i really appreciate it, the egg sprite is an animation, what would i say in code to destroy that, would it bw destroy(this.anim) ? i also have the animation of the impact effect attached, do i need to activate via the animator or would a line of code suffice??
     
  5. harveyshideout

    harveyshideout

    Joined:
    Jan 12, 2014
    Posts:
    33
    would this remove the animation?
    spriteRenderer.enabled =false;
     
  6. harveyshideout

    harveyshideout

    Joined:
    Jan 12, 2014
    Posts:
    33
    usingUnityEngine;

    publicclassExplosion:MonoBehaviour{voidOnAnimationFinish(){Destroy(gameObject);}}

    stick this after, should do the job.

    I just looked at your monkey game, quite good, how long did it take you?
     
  7. Outwizard

    Outwizard

    Joined:
    Jun 25, 2015
    Posts:
    22
    A sprite is a sprite and animation an animation. Can't say that a sprite is an animation :p. Activating via animator is usually easier, for example
    Code (CSharp):
    1. animator.SetBool("collected", true);
     
  8. Outwizard

    Outwizard

    Joined:
    Jun 25, 2015
    Posts:
    22
    This would probably make your animation invisible if it's tied to the spriteRenderer that you are disabling. But not remove it.
     
  9. Outwizard

    Outwizard

    Joined:
    Jun 25, 2015
    Posts:
    22
  10. harveyshideout

    harveyshideout

    Joined:
    Jan 12, 2014
    Posts:
    33
    I got it sorted thankyou,