본문 바로가기

TIL

23.09.05

TextMeshPro

Unity 버전이 높아서 Text 대신 TextMeshPro를 사용하였는데 script를 통해서 값을 변경하려고 했는데 방식이 달라져서 기존의 방법이 통하지 않았다.

그래서 Google에 검색을 통해서 알아냈는데 아래와 같이 선언하여 사용하면 된다.

using TMPro;

[SerializeField] private TextMeshProUGUI Text;

Unity 오류 (NullReferenceException: Object reference not set to an instance of an object)

Unity를 Play했는데 아래 오류가 무한정 뜬다.

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.AnimationStateMachine.Graph.GenerateConnectionKey (UnityEditor.Graphs.Node srcNode, UnityEditor.Graphs.Node dstNode) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/Graph.cs:264)
UnityEditor.Graphs.AnimationStateMachine.Graph.GetEdgeInfo (UnityEditor.Graphs.Edge edge) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/Graph.cs:283)
UnityEditor.Graphs.AnimationStateMachine.EdgeGUI.DoEdges () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/EdgeGUI.cs:71)
UnityEditor.Graphs.AnimationStateMachine.GraphGUI.OnGraphGUI () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/GraphGUI.cs:390)
UnityEditor.Graphs.AnimatorControllerTool.StateMachineView (UnityEngine.Rect position, System.Single zoomLevel) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:1888)
UnityEditor.Graphs.AnimatorControllerTool.DoGraph (UnityEngine.Rect graphRect, System.Single zoomLevel) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:1251)
UnityEditor.Graphs.AnimatorControllerTool.<SetupGUI>m__D () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:1663)
UnityEditor.Graphs.AnimatorControllerTool.ScopedOnGUI (System.Action onGUIHandler) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:1423)
UnityEditor.Graphs.AnimatorControllerTool.<SetupGUI>m__5 () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Animation/AnimatorControllerTool.cs:1660)
UnityEngine.Experimental.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout) (at C:/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:266)
UnityEngine.Experimental.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect) (at C:/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:438)
UnityEngine.Experimental.UIElements.IMGUIContainer.HandleIMGUIEvent () (at C:/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:412)
UnityEngine.Experimental.UIElements.ImmediateStylePainter.DrawImmediate (System.Action callback) (at C:/buildslave/unity/build/Modules/UIElements/ImmediateStylePainter.cs:113)
UnityEngine.Experimental.UIElements.IMGUIContainer.DoRepaint (UnityEngine.Experimental.UIElements.IStylePainter painter) (at C:/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:98)
UnityEngine.Experimental.UIElements.VisualElement.Repaint (UnityEngine.Experimental.UIElements.IStylePainter painter) (at C:/buildslave/unity/build/Modules/UIElements/VisualElement.cs:862)

NullReferenceException: Object reference not set to an instance of an object

해당 오류가 발생해서 열심히 찾아보고 있었다. Object reference not set to an instance of an object 오류는 참조를 잘못했을 때 발생한다고해서 GameManager의 코드도 열심히 들여다 보고, Hierarchy에서 각 오브젝트마다 잘못 참조된 부분이 있는지 찾아보았지만 아무런 오류를 찾을수 없었다.

UnityEditor.Graphs.AnimationStateMachine.Graph.GenerateConnectionKey

그래서 Object reference not set to an instance of an object 의 아래에 있는 오류를 자세히 들여다 보았다. 중간에 Animation이 있는걸 보니까 Animation 부분이 잘못됬다는걸 알 수 있었다.

UnityEditor.Graphs.AnimatorControllerTool.StateMachineView

그보다 아래를 내려보니 AnimatorController라는 부분이 보여서 AnimationController에 관련됬다고 생각했다.

그래서 AnimationController 파일에 들어가 보았더니 해당 오류가 발생하는게 아닌가

결론

이 오류는 Unity 자체 오류로 그냥 껐다 키니까 해결되었다.


하위 오브젝트 찾기

GameManager 오브젝트에 여러가지를 다 필드로 선언해서 넣는것 보다 상위 오브젝트 1개만 넣고 하위 오브젝트는 그때그때 찾아서 값을 가져오거나 변경하는게 더 간편할꺼 같아서 기능을 넣었다.

GetComponentInChildren

처음에는 GetComponentInChildren 메소드를 사용하였는데 이걸 사용하면 하위 오브젝트에 해당하는 Component가 여러 개면 가장 상위에 있는 Component만 선택된다는 단점이 있다.

FindChild

그래서 Component를 찾기 보다는 하위 오브젝트를 이름으로 찾기위해서 FindChild 메소드를 사용하였는데 노란색 밑줄에 아래와 같은 경고 메시지가 나타났다.

'Transfrom.FindChild(string)' is obsolete: 'FindChild has benn deprecated. Use Find instead (UnityUpgradable)

간단하게 FindChild 메소드는 옛날 메소드니까 Find 메소드를 대신해서 사용하라는 것이였다.

하지만 FinChild 메소드 대신 Find 메소드를 사용하였는데 오브젝트를 제대로 찾지 못하였다.

GetComponentsInChildren

GetComponentsInChildren 메소드는 GetComponentInChildren 과 거의 비슷하지만 해당하는 Component를 전부 찾아서 배열형태로 반환한다는 점이 다르다. 따라서 여러 개의 Component를 사용한다면 순서를 생각해서 배치해야한다.

'TIL' 카테고리의 다른 글

23.09.13  (0) 2023.09.14
23.09.12  (0) 2023.09.14
23.09.11  (0) 2023.09.14
23.09.07  (0) 2023.09.07
23.09.06  (0) 2023.09.06