Parent/Child relationship

Before we make our first prefab, a concept that is both useful and important is parentage. In our Hierarchy view, we can have GameObjects with other GameObjects attached to them in a Hierarchy. We call the primary objects as parents, and the secondary objects are called children. When these hierarchies exist, there are many benefits from an organizational standpoint as well as for practical application.

Where there is a series of objects connected together in such a manner, if something on the parent object is changed--such as being moved, rotated, or scaled--all the attached objects do the same in relation to the parent.

Parenting is something that also applies to prefabs. With that in mind, we will parent our objects in the scene together and make a prefab from them; let's perform the following steps:

  1. Take a look at our Hierarchy view; we should still have Platform, Hello, and World:
  1. Select Hello and drag it up onto Platform until you see a light blue oval over the destination. Then, release the mouse button:
  1. Now, do the same thing for the World object. After you are done, you should see this in your Hierarchy view; your Hello and World objects are now children of Platform:
If you click on the Play button now to test your app, even after parenting the Hello and World objects, they will fall onto the Platform.

Here's a quick test to see the effect on the children when we transform the parent. Let's look at the following steps:

  1. With Platform selected, move your mouse to the Transform component in the Inspector. Click in the Z-axis box. Change the number in the field to 4:
  1. Now, click on and drag the Z axis left and right to move the platform:

As you can see, the words follow along without any issue. If you were to do the same thing to the scale on the X axis, it would squeeze and expand.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.135.224