- Home /
Unparenting Changes Object's Location
I have an several objects as a children of a parent object and I want to unparent 2 of the children.
transform.parent = null;
The problem is that when I unparent the two objects, they instantly move to the global coordinate equivalent of their local coordinates (let me explain).
Let's say that the parent object is located at 100, 100, 100. Also, let's say that both the children objects are at 1, 1, 1 (which is relative to the parent). When the two children are unparented, they move to 1, 1, 1, but now they are global coordinates.
The result is that as soon as the two children are unparented, they instantly move to a unwanted location. The goal is to keep them in the exact same location as before they were unparented. Is this possible?
I realize that I did not explain very well, let me know if/how I can clarify.
Thanks.
do they have some sort of script setting their position?
That's not something that happens by itself; unparenting doesn't change any object's location unless you're doing something to make that happen.
I don't have any code besides the line above. If you just look in inspector when the objects are unparented, it looks like nothing happened (the coordinates stay the same). But when the objects are unparented, the coordinates seem to become global coordinates ins$$anonymous$$d of relative to the parent object.
This causes them to instantly move to another (unwanted) position when they are unparented.
Am I doing something wrong?
I would say yes, but I don't know what. I haven't seen any behavior like that under any circumstances.
@AppTechStudios @Seth-Bergman @Eric5h5
I am facing the same issue please help if you found any solution to this. Basically I have a prefab which has my "player" & a "PlayerController" as a childobjects to the prefab. playerController is basically 6 UI buttons that control the direction of the player by using the onClick events. but dont know why this setup doesn't work when the "player" and the "playerController" is childed to some parent. onClicking the UI buttons with the same scenario make the player move indefinitely in opposite direction. but this dosent happen when player and playerController are independent objects... please tell me WHY????? this is why i am trying to unparent this 2 things form the prefab instance.
Answer by AppTechStudios · Nov 11, 2012 at 05:26 AM
I found the solution. Oddly enough, I simply had to uncheck the "Play Automatically" box in the animation component of the parent object. Now, when the children are unparented, they stay in the same spot as expected.
Answer by DaveA · Nov 09, 2012 at 09:30 PM
That is odd, but you could force it like this:
Vector3 temp = transform.position; // world pos
transform.parent = null; // *should* not move, but you say...
transform.position = temp; // restore world position
First off, I couldn't get the first line to work (it errored so I replaced the Vector3 with var and the error went away). I tried checking the unparented object's positions just after I unparented them and the positions are different from those shown in inspector.
So what happens in the scene and what inspector says seems abnormal. When I print out the objects' positions through a script, the coordinates are different and normal. I have no idea why this would happen.
I tried a simple experiment to see if this same behavior is apparent in other situations. It behaved normally (unlike the situation I am trying to resolve).
Is there any explanation for this?
Answer by kubajs · Mar 10, 2017 at 07:03 AM
Have you found the solution? I see the same problem now. I don't change any player rotation manualy. Just jump on rotated platform (parent player to platform) and when jumping out (unparenting), the global player's rotation is changed.
I can probably solve it using some rotation offset but I don't understand why this happens.
Problem solved. Eric5h5 was right! Thanks Eric!
I retested the scenario with "clean" objects and couldn't re-create the issue. Then I immediately went and switched off suspected scripts. I realized Player Smooth $$anonymous$$ouse Look script (the only script I haven't written myself) was causing the issue. Once I disabled the script, everything works great! This is not the first problem this script caused. First one was terrible rigidbody flying like crazy under some circumstances. Now I start to understand the reason why ;).
The lesson is: 1. Don't depend on foreign scripts or you'll spend much more time with solving problems than to write your own script. 2. When having strange issue, try to recreate it with another (clean) objects and don't spend 2 hours on that like me...
Thanks guys.
Answer by Ceachi · Mar 21, 2017 at 10:07 AM
Hello, here is an eazy way: 1) When you want to change the pozition of the object while you are in parent use for example transform.localPosition. Let say cube.transform.localPosition = new Vector3(-distance, 0, 0); Now when you unparent with cube.transform.parent=null , the global pozition will not be changed.
Answer by Cambesa · Jul 28, 2021 at 05:30 PM
In my case it was displaced because of some hidden variables in IK. Visually it was correct, values in inspector were correct. But internally some values were displaced because of a new parenting structure and animations that were built without that parent. My solution was making a seperate object instead of using the original object.