GameObject
GameObject는 Unity 씬에서 가장 기본적인 존재 단위입니다. 자체 로직이 없는 빈 컨테이너이며, 모든 동작은 부착된 Component에서 옵니다. Hierarchy·Instantiate·FindWithTag·Destroy와 참조 캐시의 기초를 배우는 입문 가이드입니다.
상상해 보세요...
GameObject는 무대 위 대본 없는 배우와 같습니다. 혼자서는 아무것도 못 하고, Component(Rigidbody, Collider, Script…)가 의상·무기·대본이 되어 비로소 움직입니다.
개념 자세히
Unity의 Entity-Component 아키텍처에서
GameObject는 Entity——자체 로직이 없는 식별자입니다. 모든 동작은 부착된 Component에서 옵니다.
모든 GameObject에는 항상 Transform이 있습니다——제거할 수 없는 컴포넌트로 위치·회전·스케일을 담당합니다. 다른 Component는 Inspector의 Add Component로 추가합니다.
GameObject는 트리형 Hierarchy에 존재합니다. Parent를 움직이면 모든 Child도 함께 움직입니다——많은 파트로 구성된 복잡한 캐릭터를 하나로 다루는 메커니즘입니다.
성능: GameObject.Find()는 씬 전체를 순회합니다. 반드시 Awake()에서
참조를 캐시해 프라이빗 필드에 저장하세요.
구조 다이어그램
⚙ Transform
Position · Rotation · Scale
필수
⚙ Rigidbody
Mass · Drag · Gravity
⚙ Capsule Collider
Center · Radius · Height
⚙ PlayerCtrl (Script)
moveSpeed · jumpForce
Custom
실습 단계
새 GameObject 만들기
Hierarchy → 우클릭 → Create Empty, 또는 내장 도형(Cube, Sphere…)을 고릅니다.
이름 짓고 Tag 지정
Inspector에서 이름을 정하고(예: Player), Tag를 지정해 스크립트가 더 빨리 찾게 합니다.
Component 부착
Inspector → Add Component → Rigidbody, Collider, 그리고 C# 스크립트를 추가합니다.
코드에서 사용
Instantiate()로 복제, FindWithTag()로 검색, Destroy()로 제거합니다.
인터랙티브 시뮬레이터
Hierarchy에서 GameObject를 클릭해 검사하세요. 생성·삭제도 시험해 보세요.
Hierarchy
← GameObject를 선택하세요
코드 예제
기본using UnityEngine;
public class GameObjectBasics : MonoBehaviour
{
public GameObject enemyPrefab;
void Start()
{
// Spawn an Enemy at (2, 0, 0)
GameObject newEnemy = Instantiate(enemyPrefab,
new Vector3(2f, 0f, 0f),
Quaternion.identity);
newEnemy.name = "Enemy_001";
// Find by Tag (faster than Find by name)
GameObject player = GameObject.FindWithTag("Player");
// Destroy the enemy after 5 seconds
Destroy(newEnemy, 5f);
}
}코드 예제
고급using UnityEngine;
public class GameObjectAdvanced : MonoBehaviour
{
[SerializeField] private GameObject bulletPrefab;
[SerializeField] private Transform firePoint;
// Cache components — avoid GetComponent() in 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);
// Keep Hierarchy tidy: parent the bullet under a container
GameObject pool = GameObject.Find("BulletContainer");
if (pool != null) bullet.transform.SetParent(pool.transform);
// Disable without freeing memory
gameObject.SetActive(false);
}
}📌 빠른 정리
- ▸GameObject = 빈 컨테이너; 동작은 Component에서
- ▸모든 GO에 Transform이 있음——제거 불가
- ▸
SetActive(false)는 숨기지만 메모리는 해제하지 않음 - ▸이름보다 Tag로 찾는 편이 빠름
⚠️ 흔한 실수
❌ Update() 안에서 GetComponent<>() 호출
매 프레임 실행——매우 비쌈
✅ Awake()에서 프라이빗 필드에 캐시
❌ Update() 안에서 GameObject.Find() 호출
매 프레임 Hierarchy 전체 순회
✅ [SerializeField]로 Editor에서 참조를 드래그