x


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

more ▼

asked Dec 03 '11 at 09:13 AM

Stef gravatar image

Stef
16 14 20 22

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
    }
}
more ▼

answered Dec 03 '11 at 11:01 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Ah! I did not see that either. I blame the poor formatting ;)

Dec 03 '11 at 11:41 AM OrangeLightning
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x118
x12

asked: Dec 03 '11 at 09:13 AM

Seen: 900 times

Last Updated: Dec 03 '11 at 11:41 AM