GameObject
GameObject là đơn vị tồn tại cơ bản nhất trong Unity Scene — một thùng chứa trống không có logic riêng; mọi hành vi đều đến từ các Component được gắn vào.
Hãy tưởng tượng...
GameObject giống như một diễn viên chưa có kịch bản đứng trên sân khấu. Bản thân anh ta không làm gì cả — chính các Component (Rigidbody, Collider, Script...) mới là trang phục, vũ khí và kịch bản biến anh ta thành nhân vật có hành vi thực sự.
Khái niệm chi tiết
Trong kiến trúc Entity-Component mà Unity xây dựng,
GameObject là Entity — thực thể định danh không có logic. Mọi hành vi đều đến từ
Component gắn vào nó.
Mỗi GameObject luôn có sẵn Transform — component không thể xóa, quản lý vị trí, góc quay, tỷ lệ. Ngoài ra bạn có thể gắn thêm bất kỳ Component nào qua Add Component trong Inspector.
GameObject tồn tại trong một Hierarchy dạng cây. Khi bạn di chuyển Parent, tất cả Children di chuyển theo — cơ chế tạo nhân vật phức tạp nhiều bộ phận hoạt động như một khối.
Về hiệu năng: GameObject.Find() phải duyệt toàn bộ Scene. Luôn
cache tham chiếu trong Awake() và lưu vào
biến private.
Sơ đồ cấu trúc
⚙ Transform
Position · Rotation · Scale
Bắt buộc
⚙ Rigidbody
Mass · Drag · Gravity
⚙ Capsule Collider
Center · Radius · Height
⚙ PlayerCtrl (Script)
moveSpeed · jumpForce
Custom
Hướng dẫn thực hành
Tạo GameObject mới
Hierarchy → chuột phải → Create Empty hoặc chọn shape sẵn (Cube, Sphere...).
Đặt tên và gán Tag
Đặt tên trong Inspector (vd: Player), gán Tag để script tìm kiếm nhanh hơn.
Gắn Components
Inspector → Add Component → thêm Rigidbody, Collider, rồi script C# của bạn.
Dùng bằng Code
Instantiate() tạo bản sao, FindWithTag() tìm kiếm, Destroy() xóa.
Trình mô phỏng tương tác
Click vào một GameObject trong Hierarchy để xem Inspector. Thử tạo hoặc xóa đối tượng.
Hierarchy
← Chọn một GameObject
Ví dụ Code
Cơ bảnusing UnityEngine;
public class GameObjectBasics : MonoBehaviour
{
public GameObject enemyPrefab;
void Start()
{
// Tạo một Enemy tại vị trí (2, 0, 0)
GameObject newEnemy = Instantiate(enemyPrefab,
new Vector3(2f, 0f, 0f),
Quaternion.identity);
newEnemy.name = "Enemy_001";
// Tìm bằng Tag (nhanh hơn Find theo tên)
GameObject player = GameObject.FindWithTag("Player");
// Xóa enemy sau 5 giây
Destroy(newEnemy, 5f);
}
}Ví dụ Code
Nâng caousing UnityEngine;
public class GameObjectAdvanced : MonoBehaviour
{
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private Transform firePoint;
// Cache component — tránh GetComponent() trong Update
private Rigidbody _rb;
private Animator _anim;
void Awake()
{
_rb = GetComponent<Rigidbody>();
_anim = GetComponent<Animator>();
}
public void SpawnBullet()
{
if (bulletPrefab == null) return;
GameObject bullet = Instantiate(bulletPrefab,
firePoint.position, firePoint.rotation);
// Tổ chức Hierarchy: đặt bullet vào container
GameObject pool = GameObject.Find("BulletContainer");
if (pool != null) bullet.transform.SetParent(pool.transform);
// Bật/tắt không xóa khỏi bộ nhớ
gameObject.SetActive(false);
}
}📌 Ghi nhớ nhanh
- ▸GameObject = "thùng rỗng", hành vi đến từ Component
- ▸Mọi GO đều có Transform — không thể xóa
- ▸
SetActive(false)ẩn object mà không xóa bộ nhớ - ▸Dùng Tag thay tên để tìm kiếm nhanh hơn
⚠️ Lỗi thường gặp
❌ GetComponent<>() trong Update()
Chạy mỗi frame, cực kỳ tốn tài nguyên
✅ Cache vào biến private trong Awake()
❌ GameObject.Find() trong Update()
Duyệt toàn bộ Hierarchy mỗi frame
✅ Dùng [SerializeField] kéo thả trong Editor