- Home /
undoing parenting of object's transform
I made the transform of objectB the parent of objectA, to enable object A move along with objectB. As shown below
objectA.transform.parent = objectB.transform
Now, there is need to reverse the process, so that objectA can move on its own.
Regards
@AlwaysSunny: A passenger enters a train and at a particular point, the train stops for the passenger to alight. When she enters the train, I decided to set her transform to that of the train, then there is need to reset her transform back, so that she can alight and move separately from the train. this was my reason for using the term parenting and un-parenting.
Setting the transform to null did not work. And if decided to detach the children- using objectB.transform.DetachChildren(), It would detach all the children, which is not what I want.
Though i have found a way round it, though not the best result for me. i decided to set the vector controlling the movement-speed, direction. for both the train and the passenger to the same. So they both move together, and at stopping point, the passenger was able to alight.
I believe there should be a better way of handling this.
Regards Olumyde
First of all this doesn't really answer the question at the top, so this shouldn't really be an answer. Second if your passenger wasn't a child of another object before you made it a child of the train, the parent of the passenger was null. Setting the passenger's parent value to null will detach the passenger from the train and it can move on it's own again. Saying "didn't work" isn't a proper explanation of what happens or what exactly didn't work.
You were told to just set the parent to null. It seems you've added this strange line:
objectA.tranform.parent = objectA.transform;
which makes no sense at all. The object can't be a child of itself. The parent variable builds a hierarchy tree. If an object doesn't have a parent it's at the top of that hierarchy and doesn't depend on anything else.
Answer by Itaros · Nov 08, 2014 at 12:56 PM
objectA.transform.parent = null
Thanks a lot
Using null actually delete the transform, but when i tried reverting back to the previous transform, that is the original transform of objectA, It was still attached to object B
objectA.transform.parent = objectB.transform
later,
objectA.tranform.parent = null;
objectA.tranform.parent = objectA.transform;
Why would you attempt to set a transform to be its own parent? That makes no sense. Neither does this comment - I have no clear picture of your scenario or problem. Please consider revising your phrasing using proper jargon.
@olumide: I don't get that either. Setting the parent of an object to null won't delete or destroy anything. It just makes the object a root object without a parent. Also, like AlwaysSunny said, you can't set the parent of an object to itself. That doesn't make much sense.
Answer by SmalleeStudio · Nov 10, 2014 at 05:29 AM
objectB.transform.DetachChildren();
While it will work, this will remove the parent from all children. Probably not what the OP intended.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Separate child rotation 1 Answer
Assign parent object to target 2 Answers
Object scale/rotation changes when parented to flipped object 0 Answers
Instantiate a Prefab as child 0 Answers