In Unity setting the parent GameObject for items in your game is an important feature to understand. This can be used for picking up weapons, carrying items, or tethering two objects together. Child objects can appear to behave in strange ways if you do not understand their positioning and how it relates to the parent or game world you have created. In this Tips and Tricks will show you how to properly set the parent GameObject using the method SetParent in Unity.
In the Unity Documentation for SetParent we can find the implementation for the method. It is a method that is called on the Transform of a GameObject. Every object that you have placed in a scene has this component. Transform specifies where an object is in the scene, how it is rotated, and what the scale is from the object’s original size. Transform also contains a property for tracking a parent object.
Furthermore, the Transform.SetParent method can take two values. First, is the Transform of the parent. Second, a boolean to signify whether the GameObject should adjust to the parent’s transform, again that is position, rotation, and scale, or should it stay relative to the world, which we will elaborate on soon.
At this point, you may be wondering “what is a parent GameObject”. The easiest way to explain a parent is to think of it as a container or a box. If you move the box the things inside of it move, if you attach a water cannon to the box it can now shoot water, if you put a snake in the box you will get a plot about stealth operatives, and if you destroy the box everything inside of it is destroyed.
To begin, this demonstration has a fairly simple setup. We have a class named TTSetParent that contains two public fields, a boolean and a transform. Any GameObject with this script attached will set its parent with the provided data on Start. Here is the entire script.
public class TTSetParent : MonoBehaviour
public bool worldspace;
public Transform parent;
In addition to the script, we have created four sprite assets of various shapes and colors. Then, we attached our script to the triangle and square sprite shapes and assigned the circle sprite shape to the parent field for each and left the Worldspace checkbox unchecked.
Circle – Position is set to (0, -2), to place it slightly below center, and color set to blue.
Triangle – with position set to (0, 2), to place slightly above center, and color set to red.
Square – Its position is set to the same as the triangle, so that they overlap, and the color set to black.
Hexagon – Set to position (0, 0) and color set to yellow. This will represent the center of our game world and will not change.
Now, we can run our demo in the editor. You will immediately notice that both the triangle and square move to the center of the screen and overlap the yellow hexagon. Select either the triangle or the square and observe the position coordinates in the inspector. They are still set to (0, 2). Do the same for the hexagon and you will notice that it is also still set to (0, 0), but how are these GameObjects occupying the same space?
In spite of these objects being visually in the same space in the scene, they are referencing different points of origin. For the triangle and square, which are now child objects of the circle, their world centers around the circle, while the hexagon centers around the world position of (0, 0).
To demonstrate how world position can impact your objects placement, select the triangle GameObject and click the check box for Worldspace. This will set our world space value to true. With that done, select play in the Unity editor to see what impact it will have.
This time, the triangle stayed where it was at while the square object moved to cover our hexagon in the center of the game world. Select the triangle object in the hierarchy and view the position in the inspector. Something different has happened, the Y coordinate shows 4.
Now, to walk through what is going on. The triangle started with a Y position of 2. When it was parented to the circle without world space, the Y position stayed at 2 but was in the world Y position of 0. With world space the Triangle knows it needs to be at world Y position of 2 but is now referencing the circle as it is center. Unity has calculated what the Y position should be based on where the circle is and where you placed it in the world. The circle is at the world Y position -2, so for the triangle to appear at world Y position 2, while still being a child of the circle, it has to be 4 above the parent’s center. Thus -2 + 4 = 2.
To continue, we can further complicate things by adding rotation into the mix. Select the circle GameObject and change the Z rotation to 90. Hit play in the editor to see what impact this will have.
The square is now to the left of the circle but again the position values have not changed, despite being in an entirely different place. The Triangle continues to hold its place visually in the scene. Viewing the triangle GameObject in the inspector shows the new position values calculated by Unity to maintain the (0,2) position in world space while using Set Parent to turn the object into a child. The Z rotation has adjusted to -90 to offset our 90 rotation on the parent circle. Also, the X and Y position is now set to (4, -4.768).
Finally, we can take these same principles and apply them to the scale property. Set the circle X scale to 2.5 and click play in the Unity editor. We will now see that the square is stretched into a rectangle and while, again, maintaining the same values it was originally set to by becoming relative to the parent circle. The triangle however has reduced its scale to maintain its values in world space.
And now you are ready to parent GameObjects while making games in Unity. Thank you for stopping by. Stick around and check out more of our tutorials or posts like our piece on Tips and Tricks: Unity Instantiate Prefab as Child of GameObject. Also, leave a comment telling us what you liked or did not like about the tutorial. Was it easy to follow along? What do you want to learn next? As always check out some of our published apps below.