Introduction
Parent and child GameObjects are essential concepts to grasp while developing a game in Unity. Almost every game will rely on frequent changes in parent-child relationships, so understanding what these interactions accomplish - and how to build them in the first place - is a vital lesson for a rookie programmer.
Children are GameObjects inside the hierarchy of Unity, which is a program built on ordered hierarchies. Children are confined by their parents and are susceptible to the parent's placement, rotation, and scale, among other things. Once a child is generated, its parent heavily governs GameObject.
Parenting
When using Unity, one of the most fundamental things to grasp is parenting. When one GameObject is the Parent of another, the Child GameObject moves, rotates, and scales precisely like its Parent. Parenting may be compared to the interaction between your arms and your body; anytime your body moves, so do your arms. Child items can have their children, and so on. So your hands may be seen as "children" of your arms, with each hand having many fingers. There can be several children for each item, but only one parent. These several layers of parent-child interactions form a Transform hierarchy.
The root is the object highest in a hierarchy (i.e., the only item in the hierarchy that does not have a parent).
Drag any GameObject in the Hierarchy View onto another to create a Parent. The two GameObjects will now have a Parent-Child connection.
Source: Unitydocs
The Transform values in the Inspector for every child GameObject are presented relative to the Transform values of the Parent. These values are known as local coordinates. Continuing to the analogy of body and arms, your body's location may change as you walk, but your arms will remain attached at the same relative position. Working with local coordinates for child items usually is adequate for scene development. Still, in gaming, it is frequently helpful to discover their actual position in world space or global coordinates. The Transform component's scripting API contains distinct properties for the local and global position, rotation, and scale and the ability to transform any point between local and global coordinates.
The editor can assign items a position in the hierarchy, but code can also be used.
var other = GetOtherGameObject();
other.transform.SetParent( transform );
other.transform.SetParent( transform, worldPositionStays );
When you set a transformed parent, the object's position is kept as a global position. You may make this location relative by setting the worldPositionStays option to false.
Getting a Child
Children can be found in the hierarchy because objects can be parented to one another. The following approach is the simplest way to accomplish this.
transform.Find( "other" );
transform.FindChild( "other" );
The GetChild method is another approach to getting a child.
transform.GetChild( index );
Changing Sibling Index
A GameObject's children can be rearranged in any sequence. You can do this to define the children's draw order.
other.transform.SetSiblingIndex( index );
The following techniques can easily set the sibling index to first or last.
other.transform.SetAsFirstSibling();
other.transform.SetAsLastSibling();
Detaching all Children
You can perform the following to release all transformed children.
foreach(Transform child in transform)
{
child.parent = null;
}
Additionally, Unity has a method for this.
transform.DetachChildren();