Hi all,
I’m having problem with my training project.
I have a gameobject (parent) which is attached with an other gameobject (child) in script. When i rotate parent, the child rotates, so it’s fine. But when i detach the child from its parent in script, the child rotates back to its old rotation before being attached.
Is there any difference between attaching in script and in editor.
Thanks.
PS: i attach child to parent by this script:
child.transform.parent = parent.transform;
and detach by this one:
child.transform.parent = null;
This is because child.transform.localRotation is actually Queaternion.Identity (unless changed). This is what children use to determine their rotation relative to parents.
child.transform.parent = parent.transform;
Don’t do this.
parent (lowercase) is a predetermined value inherited from GameObject.
Use Parent (capital P) if you have
public Transform parent;
defined.
It is good practice to avoid confusion, which may happen if another object wants refenrece this object’s parent from another script.
The local variable of course overwrites the inherited one, but it is good to avoid doing this.
You might want to add a line before detachign the child:
child.transform.rotation = parent.rotation;
child.transform.parent = null;
This should give the child the parent’s rotation before it’s detached.