What Is Hierarchy in Unity: Explanation and Example
Hierarchy is a panel that shows all the GameObjects in your scene arranged in a parent-child structure. It helps organize objects so that children move or behave relative to their parents, like a family tree for your game elements.How It Works
The Hierarchy in Unity works like a family tree or an organizational chart. Each GameObject can be a parent or a child. When you make one object a child of another, it means the child moves, rotates, or scales together with the parent. This helps keep related objects grouped logically.
Think of a car model: the wheels are children of the car body. If you move the car body, the wheels move with it automatically. But you can still move a wheel individually if needed. This system makes managing complex scenes easier and keeps your game objects tidy.
Example
using UnityEngine; public class HierarchyExample : MonoBehaviour { public GameObject parentObject; public GameObject childObject; void Start() { // Make childObject a child of parentObject childObject.transform.parent = parentObject.transform; // Move parentObject parentObject.transform.position = new Vector3(5, 0, 0); // The childObject will move with the parent automatically } }
When to Use
Use the Hierarchy to organize your scene objects logically and visually. It is especially useful when you have complex objects made of many parts, like characters, vehicles, or buildings.
It helps with:
- Grouping related objects so they move and behave together.
- Keeping your scene clean and easy to navigate.
- Applying transformations to a whole group at once.
For example, in a game, you might group all parts of a player character under one parent object. This way, moving the player moves all parts together.
Key Points
- The Hierarchy shows all GameObjects in a parent-child tree structure.
- Child objects inherit movement, rotation, and scale from their parents.
- It helps organize and manage complex scenes easily.
- Use it to group related objects for better control and clarity.