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 baozi · Jul 26, 2013 at 07:07 PM · destroydestroy object

Prevent destruction of an object's child

I have a simple question that I just can't seem to find an answer to. I have a script that automatically cleans up an object by calling Destroy on it. This works as intended, and proceeds to destroy the object along with its children.

However, my problem is that in a specific scenario the destroyed object may have some child object that I don't want to destroy. How would I prevent that? I figure I'd have to use OnDestroy inside the child object, but nothing I can think to add actually stops the object from being destroyed.

Thanks in advance!

Edit: Clarified title to be specific to child objects.

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 roojerry · Jul 26, 2013 at 07:24 PM 0
Share

Seems like you would want to unparent any child objects you want to keep, before destroying the parent object

like childGO.transform.parent = null;

avatar image baozi · Jul 26, 2013 at 07:29 PM 0
Share

So I'd need to include a special check inside the parent for this scenario? Is there no way to do it from the child? Thanks for the response.

avatar image roojerry · Jul 26, 2013 at 07:32 PM 0
Share

you could do the same thing from the child.

1 Reply

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

Answer by baozi · Jul 26, 2013 at 09:56 PM

I managed to solve this by using BroadcastMessage("MySpecialCheck", SendMessageOptions.DontRequireReceiver) right before the Destroy call and including a handler ("MySpecialCheck") inside the child that removes itself from the hierarchy to avoid destruction. This way I could include a child-specific handler for each object that needed an exclusion case without muddying the parent's code.

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

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

16 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 avatar image avatar image avatar image avatar image

Related Questions

Deleting objects from list sequence 1 Answer

Spawned objects randomly not destroyed 1 Answer

how to destroy a object only in game and not the prefab 2 Answers

Destroying Object without removing attached prefab, How can I destroy an object without destroying the original prefab attached? 1 Answer

How could i make it so that my bullets destroy after x amount of seconds? 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