Inflearn コミュニティ Q&A
Null Reference Exception 에러
作成
·
623
0
참조할 수 없는 오브젝트를 참조하려고 할때 해당 에러가 나온다고 하는데, 지금 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;
}
}
}
クイズ
게임 오브젝트의 이동 계산 시 Time.deltaTime 값을 곱해주는 주된 이유는 무엇일까요?
캐릭터의 최대 이동 속도를 제한하기 위해
모든 컴퓨터에서 초당 이동 거리를 일정하게 유지하기 위해
물리 엔진 계산의 정확도를 높이기 위해
오브젝트의 회전 상태에 따라 이동 방향을 결정하기 위해
回答 2
1
안녕하세요
NULL 크래시는 말 그대로 어떤 객체가 안 만들어졌거나 없는 상태이고,
버그 중에서 가장 빈번하고 쉬운 편에 속하니,
연습이라 생각하고 디버깅을 해보면서 고민해보시길 추천드립니다.
17라인에 BP를 잡아 실행하다보면,
s_instance가 만들어지지 않은 것을 확인하실 수 있을거에요.
s_instance가 왜 없을까? 싶긴 하지만 코드를 보다 보면
애당초 s_instance = 무엇무엇; 으로 저장하는 부분이 없습니다.
즉 Managers mg = go.GetComponent<Managers>();
얘가 문제네요.
0





