• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    해결됨

UI_Inven에서 Bind시 오류가 발생합니다

22.07.26 05:33 작성 조회수 936

0

UI_Inven에서
Bind<GameObject>(typeof(GameObjects));를 할 때 오류가 떠서 breakpoint를 잡아보니
 
 
이 부분의 foreach문 에서 오류가 발생합니다
 
ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.
UnityEngine.GameObject.GetComponentsInChildren[T] (System.Boolean includeInactive) (at <4a31731933e0419ca5a995305014ad37>:0)
UnityEngine.GameObject.GetComponentsInChildren[T] () (at <4a31731933e0419ca5a995305014ad37>:0)
Util.FindChild[T] (UnityEngine.GameObject go, System.String name, System.Boolean recursive) (at Assets/Scripts/Utils/Util.cs:43)
UI_Base.Bind[T] (System.Type type) (at Assets/Scripts/UI/UI_Base.cs:23)
UI_Inven.Init () (at Assets/Scripts/UI/PopUp/UI_Inven.cs:21)
UI_Inven.Start () (at Assets/Scripts/UI/PopUp/UI_Inven.cs:14)
 
Bind할 때 GameObject가 아닌 Transform으로 Bind를 해보니 오류 없이 정상적으로 작동했습니다
해결은 했지만 강의에서는 GameObject를 받아서 잘 되었는데 저는 안 되는 건지 궁금해서 질문을 남겨봤습니다.. 강의랑 버젼이 달라서 그런 것인가요??

답변 1

답변을 작성해보세요.

0

ArgumentException: GetComponent requires that the requested component 'GameObject' derives from MonoBehaviour or Component or is an interface.

버전 문제가 아니고 원래 잘못된게 맞습니다.
GameObject는 Component가 아니고 
실제 부품을 조립하는 완성본 대상입니다.
Bind를 할 때 GameObject 타입이면 다른 루트로 향하게  if-else 처리가 되어 있어서
이쪽까지 들어오지 않아야 정상입니다.

1O1D님의 프로필

1O1D

질문자

2022.07.26

 

강사님 말씀을 듣고 코드를 수정해보니 작동을 하네요 감사합니다..

그런데 T를 GameObject로 받아도 이런 식으로 처리를 해주지 않으면 안 되는 건가요?

FindChild<T> 코드에 T가 GameObject일 경우 FindChild로 들어가게 하면 해결되는 건가요?

FindChild<T>는 [T라는 부품을 가진 name이라는 이름의 GameObject을 찾는] 기능이고
FindChild는 [name이라는 이름의 GameObject를 찾는] 기능이라 그렇습니다.

1O1D님의 프로필

1O1D

질문자

2022.07.26

답변 감사드립니다!