Null Reference Exception 에러
628
작성한 질문수 5
참조할 수 없는 오브젝트를 참조하려고 할때 해당 에러가 나온다고 하는데, 지금 InputManager.cs ,Managers.cs , PlayerController.cs 전부 코드상 강의와 똑같고 문제가 없습니다. 무엇이 문제일까요
아래는 코드와 에러메세지입니다.
Managers.cs에서 10번째 라인,PlayerController 17번째 라인에서 문제가 생기는데
public static InputManager Input { get { return Instance._input; } }
Managers.Input.KeyAction -= OnKeyboard;
Managers.Input.KeyAction += OnKeyboard;
이부분이네요.

/////////// InputManager
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InputManager
{
public Action KeyAction = null;
public void OnUpdate()
{
if (Input.anyKey == false)
return;
if (KeyAction != null)
KeyAction.Invoke();
}
}
//////////////////// Managers
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Managers : MonoBehaviour
{
static Managers s_instance;
static Managers Instance { get { Init(); return s_instance; } }
InputManager _input = new InputManager();
public static InputManager Input { get { return Instance._input; } }
void Start()
{
Init();
}
void Update()
{
_input.OnUpdate();
}
static void Init()
{
if(s_instance == null)
{
GameObject go = GameObject.Find("@Managers");
if(go == null)
{
go = new GameObject { name = "@Managers" };
go.AddComponent<Managers>();
}
DontDestroyOnLoad(go);
Managers mg = go.GetComponent<Managers>();
}
}
}
///////////////////////// PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField]
float _speed = 10.0f;
void Start()
{
Managers.Input.KeyAction -= OnKeyboard;
Managers.Input.KeyAction += OnKeyboard;
}
void Update()
{
}
void OnKeyboard()
{
if (Input.GetKey(KeyCode.W))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
transform.position += Vector3.forward * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.S))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
transform.position += Vector3.back * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.A))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
transform.position += Vector3.left * Time.deltaTime * _speed;
}
if (Input.GetKey(KeyCode.D))
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
transform.position += Vector3.right * Time.deltaTime * _speed;
}
}
}
답변 2
1
안녕하세요
NULL 크래시는 말 그대로 어떤 객체가 안 만들어졌거나 없는 상태이고,
버그 중에서 가장 빈번하고 쉬운 편에 속하니,
연습이라 생각하고 디버깅을 해보면서 고민해보시길 추천드립니다.
17라인에 BP를 잡아 실행하다보면,
s_instance가 만들어지지 않은 것을 확인하실 수 있을거에요.
s_instance가 왜 없을까? 싶긴 하지만 코드를 보다 보면
애당초 s_instance = 무엇무엇; 으로 저장하는 부분이 없습니다.
즉 Managers mg = go.GetComponent<Managers>();
얘가 문제네요.
UI 기능 관련 질문이 있습니다!
0
32
2
픽셀 좌표 스크린 좌표
0
32
0
전체적으로 코드 읽는게 굉장히 오래 걸리네요...
0
72
2
카메라 #2 수업 캐릭터 쓰러짐 해결
0
83
1
플레이어 움직임이 끝날때, 기울어짐 현상
0
82
1
Input 적용과 관련하여 질문 있습니다.
0
74
1
디버그 불가.. Unity에 연결 불가
0
81
1
달리기 애니메이션 에러
0
87
2
오랜만에 게임을 만들다가 사운드 관련해서 뭔가 궁금한게 있어서 봤습니다
0
65
1
GetKey 오류
0
109
2
에셋을 어디에 올려두신지 알 수 있나요?
0
106
1
재귀호출? 오류나는데 왜 이래요 ?
0
169
6
Animation blending 파라미터 계산
0
67
1
newPos와 to지점이 겹쳐져야 하는거 아닌가요?
0
96
3
오브젝트 복제하니까 자꾸 이런게 뜨면서 꺼집니다
0
87
2
질문있습니다
0
143
2
아 이거 소스코드 같은거 없나요?
0
141
3
질문 드립니다. string.IsNullOrEmpty(name)
0
86
2
@Managers 가 씬에 배치되어 있어도 문제가 되지 않나요?
0
135
2
유니티6 NavMesh 안되는분들
1
174
1
Unity6 Unity-Chan Material 마젠타 문제 해결법
7
525
1
GetComponent<Poolable> 질문드립니다.
0
101
2
UI 불러오지 못함
0
117
2
UI 자동화 #1 - Util.cs의 FindChild함수에서 component.name에 컴포넌트 이름이 뜹니다.
0
155
2





