[Unity] RequireComponent 왜 쓰는걸까?

ChangJin·2023년 12월 14일
0

Unity

목록 보기
2/17
post-thumbnail

공식 Documentation을 참고해보면..

https://docs.unity3d.com/kr/530/ScriptReference/RequireComponent.html

RequireComponent를 추가하면 요구되는 components를 의존성으로 자동으로 추가해준다.

무려 setup 에러를 피할 수 있다!
예를들어 Rigidbody에 항상 같은 GameObject가 추가되어지게 할 수 있다!


코드 참고

using UnityEngine;

// PlayerScript는 Rigidbody component를 가지는 GameObject를 필요로 한다!!
[RequireComponent (typeof (Rigidbody))]
public class PlayerScript : MonoBehaviour {
	Rigidbody rb;
	
	void Start() {
		rb = GetComponent<Rigidbody>();
	}
	void FixedUpdate()  {
		rb.AddForce(Vector3.up);
	}
}
profile
게임 프로그래머

0개의 댓글

관련 채용 정보