Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 plashivranov · Jan 18, 2021 at 08:27 AM · parentchildhierarchychildrenparent-child

Change an object's grandparent

Hello, this is my first question here, so please be patient with me. I have a hierarchy of the following type: alt text I need to be able to change a child's grandparent and parent, so that I keep the same hierarchical position. My game involves applying properties (visibility etc.) to all children and grandchildren of a certain grandparent. I need to change the object's grandparent, so that it automatically receives its properties (which is decided upon collision). Also, all grandparents have an identical hierarchy. In other words, I need to change the hierarchy from the one above to the following: alt text I know how to get the parent of a parent (`Child1.transform.parent.parent.transform`) , or set a new parent to an object (`Child1.transform.parent = GrandparentB.transform;`). However, the latter will only move Child1 to GrandparentB (GrandparentB will become its parent) and not further down the hierarchy. I suppose the solution is very simple, but so far I haven't found any help in the forums.

screenshot-2021-01-17-at-123555.png (21.2 kB)
screenshot-2021-01-17-at-123612.png (20.9 kB)
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
0
Best Answer

Answer by Llama_w_2Ls · Jan 18, 2021 at 08:40 AM

Well you can change the parent of any transform through transform.SetParent(Parent.transform); You can use GameObject.Find(), to find the parent, and then set the child as the parent of that object. For example:

 GameObject Parent = GameObject.Find("Parent1");
 Child1.transform.SetParent(Parent.transform);


@plashivranov

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 plashivranov · Jan 18, 2021 at 08:51 AM 0
Share

Thank you for the answer. I get the idea that your solution will work if all the parents have unique names. However, I'm using the same names for parents within each grandparent (there is no "Parent A" or "Parent B", there is only "Parent"). Is this a mistake? Is there any way to use the Find method only for the children of a specific grandparent?

avatar image Llama_w_2Ls plashivranov · Jan 18, 2021 at 09:22 AM 0
Share

You could keep references to the grandparent/parents in your script:

 public Transform Parent;
 public Transform GrandParent;

Is it really an issue to name them differently though? It would save you some hassle instead of doing it this way.

avatar image plashivranov · Jan 18, 2021 at 10:59 AM 0
Share

Thanks! I will give a unique name to each of the parents - you are right that it will save me trouble in the future. $$anonymous$$ucho, mucho arigato!

avatar image
1

Answer by Bunny83 · Jan 18, 2021 at 08:51 AM

In your two images you do not change the grandparent but just the parent of your child. The grandparents (the parent of the parent) does not change. Just as in real life. If you "change your parents" (i.e. being adopted by another family) you change your parents. Your new parents of course have different grandparents than your old parents. Sounds a bit strange but that's how it would work. Instead of parents you can think of any other hierarchical structure. Imagine you live an a house in france. When you move to a new house that is located in spain you don't have to change the "parent" of any of the two houses. You just move to the new house and at this point you now live in spain instead of france.


So in your case if you have a child inside the "parent" of GrandparentA, you just have to set the parent of your child object to the new parent that is located under GrandparentB.


Where and how you get the references to those gameobjects is up to you. You haven't provided any code yet.


Of course technically it would be possible to change the parent of the parent. However than the result would not look like what you have shown. Because when you change the parent of the parent, the parent object would now be a child of the new grandparent. So it would look like this:

  - Grandparent A
  - GrandParent B
        - Parent
              - Child1
        - Parent
              - Child2

So when changing the parent of the parent Grandparent A would be empty since we moved the parent over to the new grandparent. I don't think that's what you want.

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 plashivranov · Jan 18, 2021 at 09:08 AM 0
Share

Thank you for the great explanation. It is what I thought the situation is. I guess my problem is that I don't have unique names for all the parents inside a grandparent, so I need to find a way to look for a parent inside a specific grandparent. This is how it looks now: - Grandparent A - Parent 1 - Child A1 - Parent 2 - Child A2 - Grandparent B - Parent 1 - Child B1 - Parent 2 - Child B2 ... etc So as you can see, the grandparents have unique names, but not the parents. If it is not possible to find children only within a specified parent (grandparent), then I may have to rework a great deal of my game in order to make them have unique names so as to use the GameObject.Find method.

avatar image Bunny83 plashivranov · Jan 18, 2021 at 09:41 AM 0
Share

Well, if you don't care about which parent inside the grandparent you want to use as new parent for your child, you can simply pick the first child of the grandparent

 Transform child;
 Transform newGrandparent;
 
 child.parent = newGrandparent.GetChild(0);

Note that GetChild(0) won't work if there's no child at all.

avatar image plashivranov · Jan 18, 2021 at 11:02 AM 0
Share

It really is a great solution. $$anonymous$$uch appreciated. I'll try this, but it might be better to just give unique names to the parents and just get it over with. Thank you for the time and attention <3

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

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

Related Questions

How To Get List of Child Game Objects 14 Answers

Make a simple tree 1 Answer

accessing all of a colliders parents children 1 Answer

transform.childCount == 0, but a child shows up in the hierarchy. How is this possible? 2 Answers

Putting a Child Class into a List of Parent Type and then casting it back to a Child 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