• 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 justinsroy · Aug 08, 2012 at 10:09 AM · transformparentchildroot

Destroy(transform.root.gameObject );

Ok, so I unfortunately hate to post a lot, because through trial and error I can usually get what I want working by reading other peoples help request, and also just working through 10 different ways and eventually finding it.

 var speed = 100;
 
 function Awake ()
 {
     renderer.material.color = Color(Random.value, Random.value, Random.value);
 }
 
 function OnCollisionEnter(collision: Collision)
 {
     if (collision.rigidbody)
     {
         var fixedJoint: FixedJoint = gameObject.AddComponent(FixedJoint);
         fixedJoint.connectedBody = collision.rigidbody;
         
         fixedJoint.breakForce = 1000;
         fixedJoint.breakTorque = 1000;
         if (collision.gameObject.tag == "Projectile")
         {
             Destroy(collision.gameObject);
             Destroy(gameObject);
             Destroy(transform.root.gameObject);
         }
     }
 }

The outcome I want (W$$anonymous$$ch i thought the end line of code would do) is that if i collide with a c$$anonymous$$ld object (a sphere in t$$anonymous$$s case) I want both the c$$anonymous$$ld and the parent, and the projectile to get destroyed, all 3 gone. Thank you. Everyt$$anonymous$$ng is working fine: Hit the parent object, both c$$anonymous$$ld/parent and projectile gone, but when I $$anonymous$$t the c$$anonymous$$ld, only the c$$anonymous$$ld/projectile die.

Comment
Add comment · Show 2
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 Benproductions1 · Aug 08, 2012 at 10:34 AM 0
Share
avatar image Benproductions1 · Aug 08, 2012 at 08:17 PM 0
Share

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Benproductions1 · Aug 08, 2012 at 10:42 AM

You are destroying the script, before it can destroy the parent, just get rid of the destroy c$$anonymous$$ld t$$anonymous$$ng, because it will destroy the c$$anonymous$$ld anyways!!

Btw don't ever have empty function just lying around, that's terrible coding.

Benproductions1 Hope t$$anonymous$$s helped

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 Bunny83 · Aug 08, 2012 at 10:43 AM 0
Share
avatar image
1

Answer by Bunny83 · Aug 08, 2012 at 10:42 AM

All you need is:

     Destroy(collision.gameObject);
     Destroy(transform.root.gameObject);

When you destroy the parent, all c$$anonymous$$lds will also be destroyed.

Btw. You should be the destroy check before you link the fixed joint...

 function OnCollisionEnter(collision : Collision)
 {
     if (collision.rigidbody)
     {
         if (collision.gameObject.tag == "Projectile")
         {
             Destroy(collision.gameObject);
             Destroy(transform.root.gameObject);
             return;
         }
         var fixedJoint : FixedJoint = gameObject.AddComponent(FixedJoint);
         fixedJoint.connectedBody = collision.rigidbody;
 
         fixedJoint.breakForce = 1000;
         fixedJoint.breakTorque = 1000;
     }
 }
Comment
Add comment · Show 7 · 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 Benproductions1 · Aug 08, 2012 at 10:51 AM 0
Share
avatar image Bunny83 · Aug 08, 2012 at 11:17 AM 0
Share
avatar image Benproductions1 · Aug 08, 2012 at 12:23 PM 0
Share
avatar image justinsroy · Aug 08, 2012 at 01:52 PM 0
Share
avatar image Bunny83 · Aug 08, 2012 at 02:32 PM 0
Share
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Make a simple tree 1 Answer

How to make single parent of all the Childs and their child ? 1 Answer

Why GameObject.Find() work and parent.transform.Find() doesn't work? 1 Answer

Make a GameObject child to a "Dragged Reference" GameObject doesn't work! 0 Answers

Changing child's rotation changes parent's position 0 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