Jorel Amthor
Jorel Amthor

Reputation: 1294

Destroy in Unity and weird behaviors ( multiple clones )

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 ! enter image description here

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 ?

Upvotes: 0

Views: 564

Answers (1)

Everts
Everts

Reputation: 10721

I would think that since you have a sphere collider, the other object is colliding on many points when entering. Consider two spheres colliding, for the engine to report collision, the two item needs to be overlapping. If you got two spheres that is a minimum of 4 contact points, those four are calling for entering. Same with Cube. If the collider is more complex, there may be more calls.

You could try to prevent the multiple calls:

private bool sentinel = false;
void OnTriggerEnter(Collider col){
    if(this.sentinel == true){ return; }
    this.sentinel = true;
    // rest of the code
}

Upvotes: 2

Related Questions