inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part3: 유니티 엔진

Input Manager

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

287

LeeGunWoo

작성한 질문수 65

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;
        }
    }
}

 

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

 

C# unity

답변 1

1

Rookiss

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

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

0

LeeGunWoo

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

ResourceManager 클래스 관련 질문

1

26

2

몬스터 HP 게이지바 이동

0

20

1

매니저 관련 질문이 있습니다.

0

70

2

비쥬얼 스튜디오에서 unity연결이 없습니다.

0

74

2

UI 기능 관련 질문이 있습니다!

0

70

2

픽셀 좌표 스크린 좌표

0

49

0

전체적으로 코드 읽는게 굉장히 오래 걸리네요...

0

105

2

카메라 #2 수업 캐릭터 쓰러짐 해결

0

96

1

플레이어 움직임이 끝날때, 기울어짐 현상

0

100

1

Input 적용과 관련하여 질문 있습니다.

0

91

1

디버그 불가.. Unity에 연결 불가

0

98

1

달리기 애니메이션 에러

0

98

2

오랜만에 게임을 만들다가 사운드 관련해서 뭔가 궁금한게 있어서 봤습니다

0

75

1

GetKey 오류

0

128

2

에셋을 어디에 올려두신지 알 수 있나요?

0

114

1

재귀호출? 오류나는데 왜 이래요 ?

0

178

6

Animation blending 파라미터 계산

0

75

1

newPos와 to지점이 겹쳐져야 하는거 아닌가요?

0

112

3

오브젝트 복제하니까 자꾸 이런게 뜨면서 꺼집니다

0

96

2

질문있습니다

0

155

2

아 이거 소스코드 같은거 없나요?

0

152

3

질문 드립니다. string.IsNullOrEmpty(name)

0

94

2

@Managers 가 씬에 배치되어 있어도 문제가 되지 않나요?

0

139

2

유니티6 NavMesh 안되는분들

1

193

1