The question is answered, right answer was accepted
Two prefabs instantiated instead of one?
I'm making an asteroids clone. When I fire a beam from my ship, it hits the asteroids, and both the asteroid and the beam are destroyed. In their place, however, an explosion particle system prefab is instantiated. Everything works perfectly, except for the fact that two prefabs appear with each destroyed asteroid. Despite being able to play the newest, most demanding games on high or sometimes ultra settings at 60 fps, the two explosions slow my system down like crazy, which doesn't make any sense to me.
Mind you, this is a 3D game. It all takes place in a 2d plane, but I'm using 3D assets, like the ship and the asteroids.
I've tried placing a single explosion in the scene and nothing slows down. Here is the relevant part of the Asteroid script.
public Transform boomObject; //Assign prefab in inspector
void OnCollisionEnter (Collision other) {
if (other.gameObject.name == "Beam(Clone)") { //check if collision is with my beam
Destroy (gameObject); //destroys asteroid
Destroy (other.gameObject); //destroys beam
Instantiate (boomObject, transform.position, Quaternion.identity); //makes 2 explosions?
}
}
Answer by Bonfire-Boy · Dec 09, 2015 at 07:41 PM
How do you know that the call is creating 2 objects, as opposed to the function being called twice? If you haven't tried this already, add a Debug.Log to your OnCollisionEnter to check this,
Oh my god now I feel like an idiot. So I tried the log and confirmed that it was in fact being called twice, and instinctively I checked asteroid prefab for multiple colliders. I don't know how I managed it but each one has two colliders. Thanks for the tip.
Follow this Question
Related Questions
Why all instantiated objects get triggered when 1 is? 0 Answers
Destroying instantiated game objects from prefab when coliding with the ground 1 Answer
ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer
Instantiating a tile in place of another tile 0 Answers
Instantiation of my GameObjects spell (from other script) 0 Answers