• 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 /
This question was closed Apr 02, 2013 at 07:27 AM by FWCorey for the following reason:

The question is answered, right answer was accepted

avatar image
6
Question by FWCorey · Nov 14, 2012 at 05:29 AM · parentchildondestroy

Child detach and save itself when parent is destroyed

Is there a way in the OnDisable or OnDestroy event for a child to detach itself from it's parent and stop itself from being destroyed? I'm trying to increase framerate and it would be handy if certain objects could child themselves to other objects then detach again when the parent is destroyed.

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

3 Replies

  • Sort: 
avatar image
18
Best Answer

Answer by Fattie · Nov 14, 2012 at 06:16 AM

I'm pretty sure that once destruction has been ordered, you cancannott "catch" it and stop it.

So you can't do it "at the child level."

But it's very easy to do what you say at the "parent" level.

Make a empty game object AAA, and then name some children of it BBB, CCC etc.

 #pragma strict
 function Start()
     {
     Destroy(gameObject, 5.0);
     Debug.Log("I will self-destruct soon ...");
     }
 function OnDestroy()
     {
     Debug.Log("I am now self-destructing ...");
     Debug.Log("However I will 'save' the children ...");
     for (var cc : Transform in transform) cc.parent = null;
     }

It's that easy. Now, more in terms of what you were thinking, put this on the parent(s) in question

 #pragma strict
 function Start()
     {
     Destroy(gameObject, 5.0);
     Debug.Log("I will self-destruct soon ...");
     }
 function OnDestroy()
     {
     for (var cc : Transform in transform)
         cc.GetComponent(Saver).OnParentAboutToBeDestroyed();
     }

That will send the message OnParentAboutToBeDestroyed() to all children.

Now, make a script called "Saver.js"

 // this is Saver.js
 #pragma strict
 function OnParentAboutToBeDestroyed()
     {
     Debug.Log("My parent was about to be destroyed, so I moved out.");
     transform.parent = null;
     }

Simply attach Saver.js to each child. (Note that you can and likely should choose to attach it just to the first level below AAA - not below.)

So that's it. Now, note that you could also use GetComponentsInChildren here but it's a bit tricky, since it also annoyingly finds "Saver" components on itself. Further, you could use SendMessage here for a clean effect, but, my feeling is the best engineering here is that you should "manually" and carefully decide which scripts do or don't have a Saver.js component.

Again the bottom line is unfortunately there is no "On my parent is about to be destroyed" or indeed nor is there something like "don't destroy me even though someone asked for me to be destroyed"

So, simply use this line of code in the parent

for (var cc : Transform in transform) cc.GetComponent(Saver).OnParentAboutToBeDestroyed();

and then in the children, simply set parent=null .. and that will avoid the destruction.

Of course, parent=null will leave the child in the top level of your heirarchy .. you can of course move them anywhere you want for convenience. Hope it helps.

Comment
Add comment · Show 6 · 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 AlucardJay · Nov 14, 2012 at 06:24 AM 2
Share

Thumb for you (I know you love it) ! The other answers are cool, but this is just good reading (save the children) =]

And this line is probably the best method :

 for (var cc : Transform in transform) cc.parent = null;

edit : what the hey, everyone gets a thumb =]

avatar image Fattie · Nov 14, 2012 at 06:55 AM 0
Share

you ROC$$anonymous$$! :)

it drives me nuts that answers like these have so few ticks

http://answers.unity3d.com/questions/326626/games-with-multiple-maps.html

can anyone help tick that answer pls?? :O

avatar image AlucardJay · Nov 14, 2012 at 07:04 AM 2
Share

Thanks for doing all that! I went on a rampage recently posting Please Tick on all my answers, but that's what I get for just being at a skill level to only help new users. I am reciprocating your kind gesture by voting yours (where I agree, but you are usually correct or at least very informative). All the Best =]

avatar image FWCorey · Nov 14, 2012 at 07:11 AM 1
Share

I think what I'll actually do is a variation of this as a script that the child will attach to the parent if it's missing which contains the array then send a message to add itself to the list of children to save. :) Debug messages were funny as hell btw!

avatar image Fattie · Nov 14, 2012 at 07:23 AM 0
Share

HEH! Good luck all. Don't hesitate to ask a new specific question as necessary!

Show more comments
avatar image
5

Answer by PracticePad Inc. · Nov 14, 2012 at 06:08 AM

This method goes on the parent, a slight variation of: http://docs.unity3d.com/Documentation/ScriptReference/Transform.DetachChildren.html

 void OnDestroy()
 {
     transform.DetachChildren();
 }
Comment
Add comment · 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
1

Answer by IgnoranceIsBliss · Nov 14, 2012 at 05:50 AM

Object.transform.parent would be the property I'd look at - you can leaf through all of the children using a FOR loop...

 for(int x=0;x<transform.GetChildCount();x++)
 {
     transform.GetChild(x).parent = NewObject.transform;
 }

Of course, I'm not sure how this can really be used to improve framerate? You could disable the renderer on the parent object, but unless you have a LOT of redundant parents I'm not clear on the benefits.

Comment
Add comment · Show 3 · 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 FWCorey · Nov 14, 2012 at 06:11 AM 0
Share

I have a script which keeps an array of objects that must remain perfectly in sync with the player and it currently does so by copying the position every frame as well as in some cases the rotation along preset selections of axis for each. If they could simply set themselves as children then detach when the player object is destroyed, waiting for it to reappear, the positions wouldn't need to be updated and neither would the relative rotation.

avatar image IgnoranceIsBliss · Nov 14, 2012 at 10:34 PM 0
Share

If the player object is only going to be temporarily lost, would it be easier to just deactivate the parent object and all of the children, rather than destroying and re-parenting?

Then there's no creation or destruction at all and your object just needs to be re-positioned when you want to revive the player.

avatar image FWCorey · Jun 14, 2013 at 05:47 PM 0
Share

What I ended up doing was creating an empty GameObject as a "Handle" with a tag on it and the parent grabbed it, moved it to matching position and childed itself to it on spawn. Then all movement was applied to the handle and moved everything. When the parent dies it doesn't effect the other children of the handle and everything is kopacetic

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

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

Make a simple tree 1 Answer

Can a parent SetActive a child GameObject anymore 2 Answers

Moving a GameObject reffering to the coordinates of the parent Object 1 Answer

How to add child collison to parrent object 1 Answer

Count how many parents in Hierarchy? 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