- Home /
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: 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: 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.
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);
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?
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.
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!
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.
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.
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.
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
Follow this Question
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