- Home /
How to create particle system on a collision?
Hi I need to know how to emit a particle system on collision. For example I have a sphere and my character has a player, I have it so when the player hits the sphere it gets destroyed and a particle system emits. But it won't work the code i'm using is below. Thanks for your help(:
var explosionPrefab : Transform;
function OnCollisionEnter(collision : Collision) { if (collision.gameObject.tag == "Hammer") { Debug.Log("Die ball!"); Instantiate(explosionPrefab); Destroy(gameObject); } }
Answer by Kiloblargh · Feb 27, 2013 at 07:36 PM
You weren't using Instantiate the right way.
var explosionPrefab : Transform;
function OnCollisionEnter (collision : Collision) {
if (collision.gameObject.tag == "Hammer") {
Debug.Log("Die ball!");
var boom : GameObject = Instantiate(explosionPrefab, transform.position, Quaternion.identity);
Destroy(gameObject, 0.1); // also sometimes destroying right away causes problems
}
}
Upvoted. I swear I just answered this same question : http://answers.unity3d.com/questions/407498/missingreferenceexception-the-object-of-type-trans-1.html
What kind of problem do you get while destroying right away? Because Update is already delayed to the end of the frame, so you are just delaying a little more with the risk that the collision could be detected again and creating a profusion of explosion since in 0.1s you can get up to 6 or 7 frames.