Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cheenodash · Feb 15, 2013 at 03:33 PM · javascriptprefabdestroy

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image paulaceccon · Feb 15, 2013 at 05:57 PM 0
Share

Could you say more? The prefab are not been destroyed? Do you want to destroy both objects?

avatar image ATMEthan · Feb 15, 2013 at 06:05 PM 0
Share

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))

avatar image Cheenodash · Feb 15, 2013 at 06:48 PM 0
Share

$$anonymous$$anaged to get it working now, cheers Paul for the comment, and thanks Ethan you advice helped me out!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cheenodash · Feb 15, 2013 at 07:35 PM 0
Share

Thank you, I'll bookmark this and implement it in the future!

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Rigidbody2D getting destroyed in the inspector 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges