Destroy not working and weird behaviors ( multiple clones )
Hi everybody,
I recently started to use Unity and i'm facing few problems. I want to create something that'll behave like a mine, thus means there would be an object and when someone will step on it, an explosion will occurs and the object that stepped on will take damage. This part works great, i've been able to figure out this behavior by myself.
After stepping on a mine, the object properly destroy itself. Therefore the "explosion" it creates remains here. Even more weird, the mine create multiple clone of this explosion. Here's the code
using UnityEngine;
using System.Collections;
public class Mine : MonoBehaviour {
Transform playerT;
GameObject itself;
// Use this for initialization
void Awake() {
playerT = GameObject.FindGameObjectWithTag ("Player").transform;
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Monster") {
EnemyHealth enemyHeatlh = other.GetComponent<EnemyHealth> ();
enemyHeatlh.Death ();
PyroExplode Pyro = gameObject.AddComponent("PyroExplode") as PyroExplode;
Pyro.setTransform(transform);
Pyro.Generate ();
Destroy (Pyro.gameObject, 1.6f);
Destroy (this.gameObject, 1.6f);
}
}
}
In this code, i instantiate a new PyroExplode, set its transform attribute and call the methode Generate() on him. This is the Generate() method :
public void Generate(){
Instantiate ( Resources.Load ("Pyro1"), mineTransform.position, mineTransform.rotation);
}
Which will load a prefab with an animation, sphere collider and script attached to it ( the script is rendering the explosion effect ). So now this is what happens when someone step on a mine
Multiples clones are created. I first thought it was because monsters were still colliding with the mine, thus recalling the OnTriggerEnter() event. I've tried to set the radius of my collider to 0 in my OnTriggerEnter() so it wouldnt collide with anything else anymore, but it doesn't help. Also, as you can see, the Pyro doesn't get destroyed. I've tried to call Destroy inside the PyroExplode class to see if it would make a difference but it doesnt.
So here are my 2 questions : 1- Why doesnt it get destroyed ? 2- Why is there more than 1 Pyro when something step on a mine ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612063557im_/https://answers.unity.com/themes/thub/images/avi.jpg)