- Home /
I made a mistake, i had a bit of code that i didn't know about and it ruined everything.
Changing the parent of an object gets rid of its parents?
So i have 3 objects, A,B, and C. A is the parent of B, and i want to set C's parent to be B. But when i use SetParent, C.transform.parent = B.transform, or instatiate C with B as the parent, in every case B stops being a child of A. I know i can just run more code every time to set A as the parent of B, but it feels like there should be a better way of doing it then that.
A and B are a part of the same Prefab and C is a seperate one. i dont know if this changes anything, but it might be relevant. Any Ideas?
EDIT; It turns out i am an idiot and forgot about this horrible bit of code i hid in C. transform.parent.parent = A; I removed this and all my problems were solved. Instantiating works, changing C's parent to B works, all of it. i just was dumb.
What code are you writing and which version of unity are you using. Because on paper there is nothing wrong with what you are doing.
i have tried
Instantiate(C,B); C.transform.parent = B.transform; C.transform.setParent(B.transform);
and all of them force B to be parentless. i have no other code anywhere in any scripts that even references B or C.
i am currently on the beta 2022.1.07b
Answer by keroltarr · Mar 05 at 08:26 PM
This may be due to the use of prefabs.
Make sure A. is instantiated (or already in the hierarchy).
Also make sure that the A that is the parent of B is the one in the hierarchy (not a prefab)
Thank you, your suggestion didn't work, but because of it i better checked all my scripts again and found the accursed line ruining everything.