Unity

[Unity][개념] GameObject, gameObject 차이

usingsystem 2022. 8. 23. 12:03
728x90

  유니티 튜토리얼 진행중에 둘의 차이점이 궁금해졌다. 어떨때는 gameObject로 사용하다가 어떨때는 GameObject로 사용하고..

  유니티 매뉴얼을 뒤져보면 GameObject는 Base Class로 Object Class를 상속 받고... blah blah....;; 역시 모든 매뉴얼은 이해하기 최대한 어렵게 써놓아야 제맛(?)인듯. 그래야 그거 쉽게 풀어논 사람이 또 돈도 벌고 책도 쓰고 그러는거 같다.

  이와 관련해서 유니티 Q&A에 fafase 라는 아이디를 가진분이 완전 자세히 둘의 차이점을 설명 해준듯.

When using gameObject, it means the script has a reference to the game object, Unity did that for you so you do not have to find the object the script is attached to. So gameObject refers to the GO the script is attached to. When a method or a member is static, and then belongs to the class and not the instance, you use GameObject like in :
 GameObject.Find("ObjectName");

as opposed to
 gameObject.GetComponent<Script>();

in the first we are doing a general action not related to the game object so it belongs to the class. You could actually called that method even though there are no GO in your scene. The second is directly related to a particular GO so we use the reference to that GO.


  간단히 정리하자면,

사용하려는 메소드나 멤버가 정적(Static)으로 선언된 것들은 직접 클래스로 사용해야 하므로 이런경우 GameObject 사용한다는 말이고,

흔히 스크립트 생성 -> 유니티에서 마우스로 드래그하여 생성된 Object에 Attach하는데,
이는 GameObject gameObject = new GameObject()으로 선언된거나 마찬가지. 그러나 유니티는 이를 알아서 수행해주므로 "So gameObject refers to the GO the script is attached to" gameObject는 attach된 스크립트로 바로 참조가 된다고 한다.


  아직은 어렵고 헷갈리는데 나처럼 초보자의 수준에서는 아래처럼 생각하는게 편한듯.

어떤 Object에 Attached된 스크립트 안에서 gameObject는 '나자신=해당Object 자신'이고 
GameObject는 '다른 Object' 이다.

지금까지의 유니티 수준에서는 GameObject를 활용하는 수준은 .Find나 .FindWithTag등이다. 즉, 나자신말고 타 오브젝트를 참조(Refer to)할때 사용.

 

http://kdhello.blogspot.com/2015/11/unityc-gameobject-gameobject.html

728x90