- Home /
OnCollision Problem
Lads, I'm trying to set up an OnCollision script so that falling chunks of debris will make a little puff of smoke particles and then be destroyed when touching the ground. I think the script looks pretty good, but I'm missing something. In actuality what is happening is that all debris objects simply disappear when I start the game. Any ideas?:
var explosionPrefab:Transform;
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "Terrain")
Instantiate (explosionPrefab,transform.position,transform.rotation);
Destroy (gameObject); }
Thanks,
Stef
Answer by syclamoth · Dec 03, 2011 at 11:01 AM
At first I didn't see it, because your post was so badly formatted, but then I realised-
You're missing brackets! This means that as soon as the object collides with anything, it immediately deletes itself, without even instantiating the explosion. Try this instead-
function OnCollisionEnter(collision: Collision) {
if (collision.gameObject.tag == "Terrain")
{
Instantiate (explosionPrefab,transform.position,transform.rotation);
Destroy (gameObject);
}
}
Ah! I did not see that either. I blame the poor formatting ;)
Your answer
