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 BilboStabbins · Sep 19, 2012 at 02:09 PM · destroyparentchildrendetach

How to have child object remain after parent is destroyed?

hi,

I have an enemy that fires an object onto a wall that appears to morph into another shape when it lands. Currently, I have this wall shape as a prefab child of the enemy with its end position set and its renderer turned off. So when the initial projectile ball is fired (instantiated as normal) at the wall and appears to collide, the renderer of the wall object is turned on. I have done it this way to avoid further instantiation of objects.

Now, my question is that as this wall object is a child of the enemy, it will get destroyed along with it when the enemy is shot. So, I was wondering what the best way would be to set it up so that the wall object remains after the enemy is gone from the scene? Possible to detach the child before destroying the enemy?

Thanks

Comment
Add comment
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

1 Reply

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

Answer by Khada · Sep 19, 2012 at 02:46 PM

Edited original answer based on feedback

Here is how to unparent a child object before you delete yourself.

 Transform child = FindChild(gameObject, "ChildName"); //Replace "ChildName" with the child objects name.
 child.parent = null;
 gameObject.Destroy();

And here is the FindChild method:

 public Transform FindChildTransform(GameObject parent, string name)
 {
     Transform child = null;
         
     foreach(Transform trans in parent.transform)
     {
         if(trans.name == name)
         {
             child = trans;
             if(child != null)
                 return child;
         }
         else
         {
             child = FindChildTransform(trans.gameObject, name);
             if(child != null)
                 return child;
         }
     }
 
     return child;
 }
Comment
Add comment · Show 5 · 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 fafase · Sep 19, 2012 at 03:34 PM 0
Share

I think it is the other way around. He wants to keep the child alive and kill the parent. Your solution unparent the child and kills it. Or am I confused?

avatar image BilboStabbins · Sep 19, 2012 at 03:50 PM 0
Share

Ah yes, so EnemyObject should be the child, not the enemy here then?

avatar image Khada · Sep 19, 2012 at 03:55 PM 0
Share

Ah, yes. Sorry about that. I'll edit the answer to reflect this.

avatar image BilboStabbins · Sep 19, 2012 at 04:55 PM 0
Share

That's perfect, $$anonymous$$hada - Thanks!

avatar image Khada · Sep 19, 2012 at 05:05 PM 0
Share

You're welcome :)

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

11 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

Related Questions

Destroyed child still is referenced. 1 Answer

Destroy(other.gameObject) isn't destroying parent, only children 2 Answers

uitoolkit slide out menu? (made for mobile but taking all ideas, except for OnGUI calls) 1 Answer

find ALL children of a parent 1 Answer

Does a child transform take the name of its parent in unity 3D? 1 Answer


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