• 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 darkhog · Oct 17, 2013 at 10:31 PM · c#instantiateitemtoolhand

How to remove all children objects & how to instantiate prefab as child to specific object

I'm making for my 3D first-person adventure game, system so you can see what item do you hold (like e.g. tools/blocks in Minecraft). However to do it, I need to know how to remove all c$$anonymous$$ld objects to specific game object ("hand" or rather pivot where items are spawning) without actually removing that object and how to instantiate object as c$$anonymous$$ld to said game object (removing/instantiating will occur rarely as game doesn't have any $$anonymous$$gh combat).

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

5 Replies

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

Answer by chillersanim · Oct 17, 2013 at 10:40 PM

The parrent-c$$anonymous$$ld concept is made with the transform property of each GameObject.
Instead of removing all c$$anonymous$$ldobjects instead of the hand, why don't you make the items be just the c$$anonymous$$ld of the hand and delete all of $$anonymous$$s c$$anonymous$$ld without worying what you delete?

Code for adding as c$$anonymous$$ld (objectA to objectB):

 objectA.transform.parrent = objectB.transform;

Code for removing all c$$anonymous$$lds from an object:

 for (var i = objectB.transform.c$$anonymous$$ldCount - 1; i >= 0; i--)
 {
     // objectA is not the attached GameObject, so you can do all your checks with it.
     var objectA = objectB.transform.getC$$anonymous$$ld(i);
     objectA.transform.parrent = null;
     // Optionally destroy the objectA if not longer needed
 } 

Greetings C$$anonymous$$llersanim

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 darkhog · Oct 17, 2013 at 10:48 PM 0
Share

Well, that's the idea. I have following structure:

 Player
 |--Camera
 |--Hand
 ...|--HandPrefab
 ...|--MaybeMoreObjects

I want to remove all children to hand so it'll look like this:

 Player
 |--Camera
 |--Hand

Then I'm getting prefab to instantiate from some global item index (already made) and want to instantiate it as child of "Hand" (name not important). So it'll look like this:

 Player
 |--Camera
 |--Hand
 ...|--HandPrefab

Then again, etc.

Also, JS belongs to browser and only because there isn't anything better until Dart or CoffeeScript will take off and be natively supported by browsers. Please give C# code.

avatar image chillersanim · Oct 18, 2013 at 07:04 PM 0
Share

My code is c#
This is not JS, and I think that it looks rather different.
The var keyWord is also used in c#, and is often used.

For what you want, you can just use my code, it should work as is.

Greetings
Chillersanim

avatar image darkhog · Nov 02, 2013 at 06:45 PM 0
Share

I have doubts. Would loop fail? As in either not removing all children or trying to remove non-existent ones and thus causing exception? You know, childCount will be decreasing and causing condition to change...

avatar image chillersanim · Nov 02, 2013 at 07:22 PM 0
Share

You're right, thanks. Updated the code, should work now.

avatar image isteffy · Jul 25, 2016 at 08:41 AM 0
Share

When the child is removed from the parent, the child is given a completely different position. This is not the solution's fault but Unity, i should clarify. Is there any fix for this?

avatar image
3

Answer by rutter · Oct 18, 2013 at 07:57 PM

By "remove" c$$anonymous$$ldren, do you mean detac$$anonymous$$ng them from the parent, or removing them from the scene?

The GameObject $$anonymous$$erarchy is determined by each transform's parent property.

Let's suppose we have two Transform references, c$$anonymous$$ld and root:

 Transform c$$anonymous$$ld = ...;
 Transform root = ...;

 //attach c$$anonymous$$ld to root
 c$$anonymous$$ld.parent = root;

 //detach c$$anonymous$$ld from root
 c$$anonymous$$ld.parent = null;

 //detach all of root's c$$anonymous$$ldren
 root.DetachC$$anonymous$$ldren();

Or, supposing we want to loop through and Destroy() all of root's c$$anonymous$$ldren:

 foreach (Transform t in root) {
     Destroy(t.gameObject);
 }

As far as creating a prefab as a c$$anonymous$$ld of an object, it's a two-step process:

  • Call Instantiate() to spawn the prefab (t$$anonymous$$s returns a reference to the spawned object)

  • Use that reference to access the instance's transform and set its parent

Somet$$anonymous$$ng like t$$anonymous$$s:

 GameObject trinketPrefab = ...
 GameObject trinket = (GameObject)Instantiate(trinketPrefab);
 trinket.transform.parent = root;
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 darkhog · Oct 18, 2013 at 10:59 PM 0
Share

Thanks!!!

avatar image sunwangshu · Jun 23, 2018 at 01:32 AM 0
Share

Thanks!!!!

avatar image
1

Answer by movAX13h · Nov 25, 2014 at 08:02 AM

Destroy all c$$anonymous$$ldren of a GameObject (container):

 w$$anonymous$$le(container.transform.c$$anonymous$$ldCount > 0)
 {
     Destroy(container.transform.GetC$$anonymous$$ld(0).gameObject);
 }
Comment
Add comment · Show 4 · 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 rootPL · May 05, 2017 at 05:14 AM 0
Share

Code above create infinite loop.

avatar image movAX13h rootPL · Oct 05, 2017 at 11:17 PM 1
Share

Code works 100%. If it does not for you, you probably forgot the ".gameObject" part.

avatar image TharosTheDragon · Oct 05, 2017 at 10:48 PM 0
Share

Why would it be infinite? Does childCount not decrease when the children are destroyed?

avatar image sunwangshu · Jun 23, 2018 at 01:33 AM 0
Share

I don't understand but this broke my code as well, even after using container.gameobject.transform.childCount for both

avatar image
0

Answer by pjj_gpu · May 08, 2019 at 01:24 AM

Notes/Warnings....

   Destroy... 
     is **NOT** immediate (as intended by Unity), 
     so if you immediately delete 3 and add 3   
     afterwards you end up with count = 6
     e.g.  null,null,null, newObj, newObj, newObj
 
    DestoryImmediate...
      changes the order in transform array as you delete ( any language t$$anonymous$$ng )
      so can't use foreach( c$$anonymous$$ld  or for( i=0....
      as noted in other answer above, must Destroy in reverse order  
     int count = transform.c$$anonymous$$ldCount;
     for (int i = count -1; i>=0; i--) {
             GameObject.DestroyImmediate(transform.GetC$$anonymous$$ld(i).gameObject);
     }


   
  


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 Develoop · Feb 27, 2020 at 06:27 PM

You can't destroy all c$$anonymous$$lds in one frame for that reason use the follow code [100% working]:

 private IEnumerator IClearItems()
     {
         for (int i = 0; i < content.transform.c$$anonymous$$ldCount; i++)
         {
             Destroy(content.transform.GetC$$anonymous$$ld(i).gameObject);
         }
         yield return new WaitUntil(() => content.transform.c$$anonymous$$ldCount == 0);
 
         // Code to initialize your new items
     }
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.

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

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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Instantiate ground/enemies 1 Answer

instantiating vertically 2 Answers

FPS Advice for Item Management 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