Playing a sprite animation before an object is destroyed
In my breakout clone, just before a block is destroyed, I would like it to display my exploding sprite animation. However, I'm lost as to how to get my animation to play, because it seems as if my animation not playing is what's preventing this coroutine I made from running (the animation doesn't play and the block remains on screen). My 1 hit, 2 hits, and 3 hits block prefabs all use this script, and those individual assets exist independently from my exploding animation. What exactly should I do? Do I have to engineer the blocks to "idol" in the animator so when they're hit, the explosion plays? Does the exploding animation have to exist in the hierarchy to some extent, because it's currently not there. Here is my script too: using UnityEngine; public class Brick : MonoBehaviour { public int maxHits; private int timesHit; public Sprite[] hitSprites; private LevelManager levelManager; public float delay = 0F; public AnimationClip explodeAnimation; private Animation myAnimation; public float explodeLifetime = 0.0F; // Use this for initialization void Start () { timesHit = 0; levelManager = GameObject.FindObjectOfType<LevelManager>(); } // Update is called once per frame void Update () { } void OnCollisionEnter2D (Collision2D col) { timesHit++; if (timesHit >= maxHits) { StartCoroutine (explode()); } else { LoadSprite (); } } void LoadSprite () { int spriteIndex = timesHit - 1; this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex]; } IEnumerator explode() { myAnimation = GetComponent<Animation> (); myAnimation.Play(explodeAnimation.name); yield return new WaitForSeconds (explodeLifetime); Destroy (gameObject); } //To Do: remove this when blocks are destructible void SimulateWin () { levelManager.LoadNextLevel(); } }
Your answer
Follow this Question
Related Questions
2D Mirroring animations not working. Unity 5. 0 Answers
Animation delay on android? 0 Answers
How too climb a ladder with the FPS Controller correctly 0 Answers