• 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 cugbpengbo · Dec 08, 2018 at 07:20 AM · script.parent-child

How to change a object's parent with it's children?

Object A has a child named B. B has many children named C, D,E,F and so on. I write a script to change B's parent to another obj named Box. When the script run, Box becomes B's parent, but B lost its children, and they all become A's children. How can I change B's parents with it's all children in the script? ,Object A has a child named B, and object b has many children named C,D,E,F....I write a script and want to change B's parent to Another object named BOX. When the script run, BOX becomes B's parent, but B lost it's children, and they all become A's children. Too wired, How to change B's parent with it's all children?

Comment
Add comment · Show 1
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 Hellium · Dec 08, 2018 at 08:36 AM 0
Share

Can you provide the code you currently have ? I don't believe calling bTransform.SetParent( boxTransform ); removes the children of B.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Vega4Life · Dec 08, 2018 at 04:30 PM

Here is an extension method that will do it for you. As you use unity for years, your extension method class should get pretty beefy. The amount I have is insane, but extremely useful. I just pull it into every project I use.


 using UnityEngine;
 
 /// <summary>
 /// Static class for our extensions - just place somewhere in your project
 /// </summary>
 public static class ExtensionMethods
 {
     // It's really a reverse recursive - If you moved from top down, the children of the children would be lost
     public static void SetParentRecursive(this Transform transform, Transform newParent)
     {
         for (int i = transform.childCount - 1; i >= 0; --i)
         {
             Transform child = transform.GetChild(i);
             child.SetParent(newParent);
         }
     }
 }


Here is a simple test script to make sure it works. Just place it on the B object, reference the Box object, then run your game, hit space bar. (Notice how the extension method is called - just like SetParent()


 using UnityEngine;
 
 
 public class TestParentingScript : MonoBehaviour
 {
     [SerializeField] Transform newParent;
 
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Space))
         {
             transform.SetParentRecursive(newParent);
         }
     }
 }
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 cugbpengbo · Dec 13, 2018 at 12:56 PM 0
Share

Thank you for your answer. I have already solved my problem, and the problem is C,D,E,F, which i wanna change parent are prefabs. I saw your method. Next time I'll try to use it to change parent. Thank you, guy!

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

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

Related Questions

Disabling game objects in game cause them to stay disabled in the scene? 0 Answers

Can not double jump when i fall off platform 1 Answer

Assign a Mesh to Mesh Collider Automatically 1 Answer

Bright, flat-rendered textures with NO lighting (no shading) - in C# code 1 Answer

Executing C# script on remote server 0 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