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 imnickb · Apr 05, 2013 at 11:01 PM · collisiontriggerdestroyparentchildren

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

I have a game object which has 10 child objects, which are all coins. I set up a killregion on the left of the screen that I want to use to destroy everything that touches it.

Here's what I'm using for the KillRegion:

 #pragma strict
 
 function Start ()
 {
 
 }
 
 function Update ()
 {
 
 }
 
 function OnTriggerEnter (other : Collider)
 {
     Destroy(other.gameObject);
 }

The individual coins are all objects with their own box colliders because I want the player to be able to collect them individually. The parent coin group object only has a transform component. The individual coins are all set to be triggers. I've read the information about Object.Destroy and I can't come up with a solution that destroys the children and the parent. I've also tried making the Kill Region a trigger but that didn't lead me anywhere promising yet. Any insight would be great! 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by foress89 · Jan 07, 2014 at 05:26 AM

Hello, I was looking for an awnser and came up with something myself, I do not know if it's the best solution but it works.

 if(other.transform.parent != null) {
      Destroy(other.transform.parent.gameObject);
 }
 else {
      Destroy(other.gameObject);
 }

My situation is that I have a box collider around my scene and if any objects gets out it gets destroyed. Now some object that required more than one collider (as childs) where not getting completely destroyed... Code snippet is self explained.

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 GetColor · Nov 05, 2015 at 05:30 AM 0
Share

I was having a very similar issue to the original poster, and your solution worked like a charm.

Thank You!

avatar image
0

Answer by EliteMossy · Apr 06, 2013 at 12:02 AM

To destroy the parent, you could do

Destroy(other.transform.parent.gameObject);

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 imnickb · Apr 06, 2013 at 12:18 AM 0
Share

That seems to be deleting the parent, but I'm randomly getting this error regarding that function:

NullReferenceException UnityEngine.Component.get_gameObject () $$anonymous$$illRegion.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/$$anonymous$$illRegion.js:16)

I'm guessing because I'm destroying the parent and then the child objects have nothing to reference? Is there a more "right" way of doing this?

avatar image imnickb · Apr 06, 2013 at 12:20 AM 0
Share

I also tried adding a collider to the whole group of coins but then I needed to add a rigid body and set that in motion as well as the child coins and it just got messier and messier. If anyone has any other ideas I'd love to hear them. Thanks!

avatar image garthhumphreys · Jun 29, 2014 at 05:40 PM 0
Share

Thank you @Elite$$anonymous$$ossy - worked for me

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

13 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

Related Questions

Player object still gets destroyed even when shields up 1 Answer

Stop an OnTriggerEnter event from happening if you quickly leave the trigger.,Is there a way to cancel an OntriggerEnter function if you leave the trigger quick enough? 2 Answers

How to have child object remain after parent is destroyed? 1 Answer

Detect collision with children 1 Answer

Is there anyway to make an object impenetrable? 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