0

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 ?

4
  • 1
    There is something strange in your code. You are adding a PyroExplode component at the component that you already into, and after that you settled the PyroExplode transform to the Mine transform, what are already the same. And finally you destroy Pyro.gameobject and Mine.gameobject, who are the same again. Correct me if I'm wrong. Commented Feb 9, 2016 at 16:31
  • I set the transform of my PyroExplode as the same as my Mine transform to avoid potential problems, it's simply to make sure that the explosion origin is the same as the mine origin ( would be weird if something land on a mine and there's an explosion few meters away ). I destroy Pyro and Mine after 1.6f, this is the length of the animation ( which means when the explosion have faded, i remove the Pyro component and the mine. The Pyro component doesnt get added to the mine hierarchy, somehow it gets added to the scene hierarchy (( that's what you see on my screenshot )) ) Commented Feb 9, 2016 at 17:00
  • The Pyro object that you see at the hierarchy is the one that is instantiated at your Generate method. The one that you destroy in the code Destroy (Pyro.gameObject); is the same GameObject that Mine is. That's because this Pyro var is a new component that you have added on runtime to the Mine object. Look, in this code PyroExplode Pyro = gameObject.AddComponent("PyroExplode") as PyroExplode; you are adding a new PyroExplode component to the SAME object that is the Mine. So it doesn't make sense you call Destroy (Pyro.gameObject); and Destroy (this.gameObject);. Commented Feb 9, 2016 at 18:12
  • Because the Pyro.gameObject and this.gameObject in that context are the same. Another thing, to make sure that the OnTriggerEnter it's not being called more than once, put a Debug.log on it. Commented Feb 9, 2016 at 18:14

1 Answer 1

2

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 } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.