x


How do I break apart an object?

Basically I have a pixelated creature made up of these cubes and when hit by a bullet I want them to fall apart. So heres the code I wrote but for some reason it does not work.

var thePrefab : GameObject;

function OnTriggerEnter (myTrigger : Collider) {
    if (myTrigger.gameObject.name == "bullet(Clone)"){
         var count : int = 0;

        while (count <= 20){
             var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
             count++;
        }

        Destroy(gameObject);
        Destroy(myTrigger.gameObject);
    }

}
more ▼

asked Aug 09 '10 at 01:59 AM

Julian gravatar image

Julian
1 1 1 1

I'd get rid of this bit (var instance : GameObject = ) because you don't use the reference. Just keep the instantiate part. Also you might want to randomise the rotation and placement a bit as you'll get 20 objects in the same spot which would lag out the game as the physics tries to sort out the displacements.

Aug 09 '10 at 01:28 PM spinaljack
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can loop through all the children in the object's hierarchy and unparent them and give them all colliders and rigidbodies.

do something like this:

for (var child : Transform in transform) {
    child.gameObject.AddComponent ("BoxCollider");
    child.gameObject.AddComponent ("RigidBody");
}

transform.DetachChildren();
Destroy(gameObject);
more ▼

answered Aug 09 '10 at 11:35 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

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

Is the OnTriggerEnter() method even triggered? Try putting a Debug.Log("blah") in there to be sure...

Do you have a collider on the character? Is it set as a trigger collider?

Is there a RigidBody component on the bullets or the character?

More info will help :)

more ▼

answered Aug 09 '10 at 12:24 PM

Cyb3rManiak gravatar image

Cyb3rManiak
1.6k 1 4 17

(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:

x5059
x1865
x1664
x764
x307

asked: Aug 09 '10 at 01:59 AM

Seen: 2225 times

Last Updated: Aug 09 '10 at 02:10 AM