- Home /
How do I destroy a prefab with a prefab?
Hey, I'm making a 2D game, which requires destroying one prefab with another. One of the prefabs is a rigidbody, the other is just a normal gameobject. The rigidbody that hits the gameobject is called Bolt and the gameobject is called enemy1. I've looked around for hours and not found anything that's worked! Cheers in advance :)
Could you say more? The prefab are not been destroyed? Do you want to destroy both objects?
I think you'll want to write a script on your bolt that: holds a reference to your enemies(something like: public List enemies or public GameObject enemy), and in the update check if it collided with an enemy, if it did you'll be able to destroy those two objcets b/c you'll have the reference to the enemy (something like: Destroy(enemy_variable_name_here.gameObject), and the reference to the bolt (something like: Destroy(gameObject))
$$anonymous$$anaged to get it working now, cheers Paul for the comment, and thanks Ethan you advice helped me out!
Answer by AndrewGrayGames · Feb 15, 2013 at 07:02 PM
The direct answer to your question is to have an OnCollisionEnter event for the projectile like so:
public void OnCollisionEnter(Collision event)
{
GameObject object = event.collider.gameObject;
GameObject.Destroy(object);
}
Your bolt can figure out from the Collision information what collider was hit, and from that, what the game object that the collider is attached to is. From there...destroy!
However, it might make more sense in terms of game rules for the collidee to self-destruct if its health/HP falls too low! Instead of calling:
GameObject.Destroy(object);
...You'd instead do something like:
object.SendMessage("TakeDamage", boltDamage, SendMessageOptions.DontRequireReceiver);
...if the entity's HP is less than or at 0, some death effect happens, and (sooner or later) the game object is destroyed.
Hope all of that helps!
Thank you, I'll bookmark this and implement it in the future!
Your answer
Follow this Question
Related Questions
Instantiate prefabs just before it comes into view 2 Answers
Instantiate prefabs before it comes into view 0 Answers
Destroy Prefab using Javascript 2 Answers
Destroy a GameObject 2 Answers