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 JVILL · Feb 02, 2013 at 06:50 AM · gameobjecttransformdestroychildclone

Destroy Child of Game Object

I checked the first 10 pages from typing "Unity Destroy child" and all didn't have the advice I was looking for.

Currently I have:

 function OnTriggerExit ( other : Collider )
 {
     outTile = other.gameObject;
     Destroy (outTile.gameObject);
 
 }

But the outTile game object has a child and I want to destroy THAT instead, not other.gameObject but other.gameObject.child (which doesn't work).

Although the other.gameObject.name can vary greatly, the child always has the same name as its parent + (clone).

Thx!

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
7

Answer by poday · Feb 02, 2013 at 08:47 AM

GameObject's can't directly access their children but every GameObject contains a Transform which can then access it's children. Something like:

 for (var child : Transform in outTile.transform) {
     if(child.name == outTile.name + "(clone)")
     {
         Destroy(child.gameObject);
     }
 }

You can reference http://docs.unity3d.com/Documentation/ScriptReference/Transform.html as Unity's documentation on Transforms.

Comment
Add comment · Show 2 · 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 JVILL · Feb 03, 2013 at 02:46 AM 0
Share

Thanks! I'll try this out.

avatar image Dustin_Morrison · Mar 24, 2016 at 06:54 PM -1
Share

Yeah this is what I was looking for! Thanks! http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

avatar image
0

Answer by DeveshPandey · Feb 02, 2013 at 08:25 AM

Try this-

 function OnTriggerExit ( other : Collider )
 {
     outTile = other.gameObject;
     Destroy (outTile.Find("ChildName"));
  }
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 JVILL · Feb 03, 2013 at 02:45 AM 0
Share

This worked, however, if there were other GameObjects with children with the same name, it didn't work. So I made the children's names unique and it was fine. Thanks!

avatar image Wolfram · Feb 03, 2013 at 03:06 AM 0
Share

Find() will do a global search in your scene, so it may cause lagging/freezing if you have a complex scene on a slow device.

@poday's approach is the way to go, assu$$anonymous$$g the child in question is a direct child of the object, and not a grandchild or deeper.

avatar image JVILL · Feb 03, 2013 at 03:26 AM 0
Share

You're right. His method worked perfectly, although I needed to change "(clone)" to "(Clone)".

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

14 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to destroy a transform's parent object. 1 Answer

Confused about GameObject and Transform 1 Answer

Neutralising child rotations -> Parent 1 Answer

Destroying all child objects. 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