• 카테고리

    질문 & 답변
  • 세부 분야

    게임 프로그래밍

  • 해결 여부

    미해결

강사님! 에러가 발생헀는데 ㅠㅠ

21.10.22 14:13 작성 조회수 202

0

제가 받아 쓴 스크립트가 문제인지 아니면 뭐가 문제인지 모르겠어요.ㅠㅠ 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Managers : MonoBehaviour
{
    static Managers s_instance; //static_instance /유일성이 보장된다. 
    static Managers Instance { get { return s_instance;  } } 
    //Property 방식으로 가져옴.

    InputManager _input = new InputManager(); 
    public static InputManager Input { get { return Instance._input; } }

    void Start()
    {
        Init();
    }

    void Update()
    {
        _input.OnUpdate(); 
    }

    static void Init()
    {
        //if(Instance == null)
        if(s_instance == null)
        {
            GameObject go = GameObject.Find("@Managers"); 
            //@Managers 옵젝의 이름을 찾는다 

            if(go == null) //만약 @Managers 옵젝의 이름이 없다면! 
            {
                //새로 생성을 해준다. 
                go = new GameObject { name = "@Managers" }; 
                //새로 생성을 하는데, 이름을  @Managers로 새로 생성
                go.AddComponent<Managers>(); //@Managers 오브젝트 생성을 했다면, Manager 컴포넌트(스크립트)를 추가해줌. 
            }
            DontDestroyOnLoad(go); //삭제가 되지 않도록 작업함!
            //Instance = go.GetComponent<Managers>(); 
            s_instance = go.GetComponent<Managers>(); 
        }
    }
}
using System; 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputManager//싱글톤으로 일반적인 C# 파일로 만들 것이다. 
{
    public Action KeyAction = null;
    //여기서 Action이 빨간 줄이 뜬다면 using System; 을 써준면 된다.
    //일종의 델리게이트 문법이다. 

    public void OnUpdate()
        //이렇게 작업을 하면 캐릭터가 아무리 1000개가 되도 이 루프마다 
        //1회만 체크!

    {
        if (Input.anyKey == false)
            return;
        //키를 입력할 경우
        if (KeyAction != null)
            KeyAction.Invoke(); 

    }
}

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] //private으로 지정된 변수를 유니티에 보이도록 작업한 것! 
    float mSpeed = 10.0f; 
    

    void Start()
    {
        Managers.Input.KeyAction -= OnKeyborad; 
        //혹여 키보드를 두번 클릭할 경우 에러 나지 않도록!
        Managers.Input.KeyAction += OnKeyborad; 
    }
 
    void Update()
    {
       
    }

    void OnKeyborad()
    {
        //키보드 WSAD으로 움직이기 
        if (Input.GetKey(KeyCode.W))
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.forward), 0.2f);
            transform.position += Vector3.forward * Time.deltaTime * mSpeed;
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.back), 0.2f);
            transform.position += Vector3.back * Time.deltaTime * mSpeed;
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.left), 0.2f);
            transform.position += Vector3.left * Time.deltaTime * mSpeed;
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Vector3.right), 0.2f);
            transform.position += Vector3.right * Time.deltaTime * mSpeed;
        }
    }
}

 

 이렇게 소스코드를 입력하고 유니티에서 재생을 해서 확인하는데 아래 이미지처럼 오류가 났다고 하는데 무슨 말인지 모르겠습니다 ㅠㅠ 도와주세요!ㅠㅠ 

 

답변 1

답변을 작성해보세요.

1

static Manager Instance { get {  Init();  return s_instance; } } 

코드가 빠져 있습니다.
NULL 크래시는 크래시 중 가장 빈번한거라, 해당 줄에 breakpoint를 잡고
왜 객체가 없는지 살펴보는 연습이 꼭 필요합니다!

LeeGunWoo님의 프로필

LeeGunWoo

질문자

2021.10.26

너무 감사합니다 ㅠㅠ 많은 연습이 필요하겠네요!