- Home /
Destroying gameobject after animation
In my breakout clone, when the the ball collides with a brick, I want the brick gameobject to destroy itself after an exploding animation plays once. However, the bricks are just constantly looping the explode animation until I hit them with the ball and they destroy themselves. I'm not sure what's wrong here, I attached the coroutine for playing the animation to the brick script. I also have no idea why this forum keeps formatting my code this way. public class Brick : MonoBehaviour { public int maxHits; private int timesHit; public Sprite[] hitSprites; private LevelManager levelManager; private Animator anime; private bool explod; // Use this for initialization void Start () { timesHit = 0; levelManager = GameObject.FindObjectOfType<LevelManager>(); anime = gameObject.GetComponent<Animator>(); explod = false; } // Update is called once per frame void Update () { if (timesHit >= maxHits) { explod = true; } else explod = false; if (explod == false){ anime.SetBool("explod", false); } if (explod == true) { anime.SetBool ("explod", true); } } void OnCollisionEnter2D (Collision2D col) { timesHit++; if (timesHit >= maxHits) { StartCoroutine (explode()); } else { LoadSprite (); } } IEnumerator explode() { explod = true; yield return new WaitForSeconds(0.1F); Destroy (gameObject); } void LoadSprite () { int spriteIndex = timesHit - 1; this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex]; } //To Do: remove this when blocks are destructible void SimulateWin () { levelManager.LoadNextLevel(); } }
Answer by FabDynamic · Oct 28, 2017 at 04:50 AM
Hey! I bet we can figure this out! First make a backup copy of everything and then here is what to try:
maxHits = 1. Set it to 1 in the inspector and default it to 1 in the code.
Change explod to a trigger (instead of a bool ). You’ll have to change it in the animation and then in the code (change setbool to settrigger)
(This one probably doesn’t matter) In the coroutine I’m pretty sure there’s no value in setting the explod there (since it is always overridden at the top of update() )
I think those changes will do the trick. if not then add debit logs at the beginning and end of every method in that class and send some screenshots of the inspector for the script and the animation window so I can figure out next steps ok?
Best wishes!
Alright I made EXPLODE a settrigger in the code and a trigger animator. I don't know how I should make explod a trigger, I can't define it as a variable. All of the individual blocks have maxhits equal to 1 and I did that in the code too. The results are still the same. `using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Brick : $$anonymous$$onoBehaviour {
public int maxHits;
private int timesHit;
public Sprite[] hitSprites;
private Level$$anonymous$$anager level$$anonymous$$anager;
private Animator animator;
private bool explod = false;
// Use this for initialization
void Start () {
print ("begin");
timesHit = 0;
maxHits = 1;
level$$anonymous$$anager = GameObject.FindObjectOfType<Level$$anonymous$$anager>();
animator = gameObject.GetComponent<Animator>();
explod = false;
}
// Update is called once per frame
void Update () {
if (timesHit >= maxHits) {
print ("update method1");
timesHit++;
explod = true;
print ("update method2");
}
else explod = false;
if (explod == true) {
print ("trigger");
animator.SetTrigger("EXPLODE");
}
}
void OnCollisionEnter2D (Collision2D col) {
timesHit++;
if (timesHit >= maxHits) {
StartCoroutine (explode());
} else {
LoadSprite ();
}
}
IEnumerator explode() {
print ("coroutine1");
explod = true;
yield return new WaitForSeconds(0.1F);
Destroy (gameObject);
print ("coroutine2");
}
void LoadSprite () {
int spriteIndex = timesHit - 1;
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [spriteIndex];
}
//To Do: remove this when blocks are destructible
void SimulateWin () {
level$$anonymous$$anager.LoadNextLevel();
}
} `