Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aca85 · Jul 13, 2017 at 09:29 PM · gameobjecttransformparentchildrenchild-to-gameobject

Change gameObjects parent at runtime

Hi all,

I have a script which at runtime moves a gameobject from one parent to another (from A to B).

  myobject.parent = ParentObject.transform;

And it works, but myobject has 2 child objects, which decouple after the move, so myobject and it's children all become siblings, the hierarchy is not preserved. How can i circumvent this? [so that when myobject changes parents, the children attached to myobject remain it's children]

Illustration of what is happening if i wasn't clear above.

 Pre move:
 -ParentA
 --myobject
 ---myobject_child1
 ---myobject_child2
 
 Post move:
 -ParentB
 --myobject
 --myobject_child1
 --myobject_child2

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

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

Answer by Garazbolg · Jul 18, 2017 at 02:11 PM

Why do you iterate through the children ? If you want your Hierarchy to stay you only need to change the parent of "myobject". Its children will follow.

So if i have :

 -ParentA
  --myobject
  ---myobject_child1
  ---myobject_child2

And I want

 -ParentB
  --myobject
  ---myobject_child1
  ---myobject_child2

I just do : myobject.transform.SetParent(ParentB)

But if i want to switch all ParentA children to ParentB and keep the hierachy under their children (Which I think is what you are trying to do) I do :

 void TransferAllA2B(Transform a, Transform b){
         bool WorldPositionStayTheSame = false;
 
         Transform kid;
         while(kid = a.GetChild(0)){
             kid.SetParent(b,WorldPositionStayTheSame);
         }
     }

Now that i think about it, you error was to think that 'GetComponentsInChildren()' apply only to the children directly underneath, but no it goes recursively, and even worst : the first array element is the object on which you called the function.

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 aca85 · Jul 18, 2017 at 02:45 PM 1
Share

Ooooh, i didin't know that, what that explains a lot of trouble i've been having. I can't wait to get home and try this, will post results.

Thnks,

avatar image aca85 · Jul 21, 2017 at 07:51 AM 1
Share

Tnx man, Tried this last night and it worked.

Had to refactor your code a bit since i use an Enumerator, but this was the key: kid = a.GetChild(0)

avatar image
-1

Answer by SuryaPrakashModi · Jul 08, 2018 at 04:10 AM

https://www.youtube.com/watch?v=XAiNddXUpH4

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
0

Answer by Cornelis-de-Jager · Jul 13, 2017 at 09:59 PM

Only change the parent of the one object not its children. So don't add / apply the script to the children objects.

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 aca85 · Jul 18, 2017 at 12:04 PM 0
Share

I am not, or at least i think i'm not. The script which is doing this is attached to Parent B, which has a gameObjcect reference to Parent A. I then iterate through all the children (not grandchildren) found in Parent A and change the parent of each child, i assumed that the children attached to myobject in Parent A wouldn't decouple after the switch.

avatar image Tony-Tonijn aca85 · Jul 18, 2017 at 12:20 PM 0
Share

How are you looping through your children? I cannot reproduce your issue when I use my implementation of iterating through the children.

avatar image aca85 · Jul 18, 2017 at 01:20 PM 0
Share
 private IEnumerator SpawnCloud()
     {        
         while (true)
         {
             _Cloud = GetCloud(ParentA);
             TransferCloudToParentB(_Cloud);
 yield return new WaitForSeconds(SpawningSpeed);
         }
     }
 
 private Transform GetCloud(GameObject parentOBJ)
     {
         var children = parentOBJ
             .GetComponentsInChildren<Transform>()
             .Where(c => c.gameObject.GetInstanceID() != PoolA.GetInstanceID())
             .ToList();
         return children[children.Count - 1];
     }
 
 private void TransferCloudToParentB(Transform cloud)
 {
 cloud.parent = ActiveClouds.transform;
 }

I added the relevant code and removed everything else, hope this helps.

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

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

Related Questions

Get Child from a parent 3 Answers

How do I reference the transform component of a gameObject to a script automatically? 0 Answers

SetActiveRecursively not activating children 2 Answers

use list of gameobjects transform in raycast 0 Answers

Make into child of a parent in c# 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