Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Parenting
2.1.
Getting a Child
2.2.
Changing Sibling Index
2.3.
Detaching all Children
3.
Setting Child Objects to Parent
3.1.
Drag and Drop
3.2.
Instantiate as Child
3.3.
Set as Child
4.
Uses of Parent and Child in Unity
5.
Frequently Asked Questions
5.1.
How can you define a Child object in Unity?
5.2.
How can we detach all Children from their Parents?
5.3.
How can we set Child objects to Parent?
5.4.
What is the easiest way to get a Child from a hierarchy?
6.
Conclusion
Last Updated: Mar 27, 2024

Parent and Child Game Objects

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();

Setting Child Objects to Parent

Here are some efficient methods for attaching GameObjects to a parent object:

Drag and Drop

The most basic way to create a child is to use the Unity hierarchy on the left side of the screen. Drag a GameObject to another GameObject by clicking on it. This will result in a tiered hierarchy, with the moved item becoming a child. It's a piece of cake. You can keep adding children to the original parent or the other children indefinitely. Dragging the kid to a blank area on the hierarchy will erase its status as a child.

Instantiate as Child

The second approach for childing a GameObject involves using a standard Unity function called Instantiation. When you Instantiate a GameObject, you're making an instance of it in the game, and in this example, you're making the model a child of an existing parent object.

You want to utilize the Instantiate() method and set the parent's Transform as the parent.

Source: Turbofuture

Set as Child

This method implies that you wish to make an existing object a child of another. It's efficient and straightforward. All you have to do with this is make sure the would-be child comes first, and the parent comes second. If this is done successfully, the first GameObject in the hierarchy will be moved behind the second. It's worth emphasizing that the parent-child relationship only exists during runtime in this scenario. When you exit the game and return to the Inspector, the parent will no longer have a child.

Source: Turbofuture

Uses of Parent and Child in Unity

The child and parent dynamic have several applications in Unity. Here are a few of the most common:

1. For simple organizational purposes - If your game includes a UI, for example, you'll most likely stack all of the UI's pieces together to make them easier to discover. You may also collapse the child item lists, which helps streamline Unity's hierarchy.

2. Facilitating easy, mass-scale changes - Assume you decide to make the UI  a little bigger or even smaller, or maybe you want to rearrange things a little or perhaps deactivate it entirely in specific cases. You may quickly achieve these changes by changing the parent rather than the individual children if you child all of the UI's components to a single parent.

3. To keep related objects together - Assume you have a character GameObject with interchangeable equipment. The equipment must travel with the character. Making every GameObject a child of the character's GameObject ensures that they remain relative to the character as they move across the screen. For animation reasons, you'll additionally need to attach child GameObjects to the parent.

4. To create new objects from precise locations - Assume your character possesses a bullet-firing gun. Bullets must spawn from the gun or maybe from an unseen GameObject at the end of the barrel. When you child, the spawn point to your parent character, the bullets fly from their specified location rather than any random position on the screen.

Frequently Asked Questions

How can you define a Child object in Unity?

Children are GameObjects inside the hierarchy of Unity, which 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.

 

How can we detach all Children from their Parents?

We can easily detach children from their parents by using the following function:

transform.DetachChildren(); 

 

How can we set Child objects to Parent?

The most basic way to create a child is to use the Unity hierarchy on the left side of the screen. Drag a GameObject to another GameObject by clicking on it. This will result in a tiered hierarchy, with the moved item becoming a child. 

 

What is the easiest way to get a Child from a hierarchy?

The GetChild method is an efficient and easy approach to getting a child

transform.GetChild( index );

Conclusion

In this article, we have extensively discussed the Parent and Child objects in Unity. We start with a brief introduction of the Object Hierarchy, then discuss Parenting in GameObjects and its uses in Unity System.

To learn more, see Operating SystemUnix File SystemFile System Routing, and File Input/Output. Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations. Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass