- Home /
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);
}
}
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.
Answer by spinaljack · Aug 09, 2010 at 11:35 AM
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);
Answer by Cyb3rManiak · Aug 09, 2010 at 12:24 PM
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 :)