
안녕하세요~ 2-10 강의를 수강 중 오류가 발생하여 질문드립니다.
제일 처음에는 강의 영상대로 코드를 그대로 작성하여 gameManager에 넣었지만, 정상적으로 작동하지 않았습니다.
그 다음에는 강의 자료에 적혀있는대로 코드를 넣어보았지만, 이번에도 최고 점수를 나타내는 숫자가 변동되지 않아 강의를 끝마칠 수 없는 상황입니다.
어떻게 해야 최고점수에 해당하는 숫자를 변동시킬 수 있을까요?
두 코드 모두 NullReferenceException 오류가 발생합니다!
강의자료 본문: https://teamsparta.notion.site/2-696062e9c0a9436eaba1cc9bdd64dc0d#681d4c5e4c8348d09296915ed6393d00
작성한 코드 및 에러 메세지
// 2-10 강의영상에 나온 코드를 gameManager에 타이핑한 경우입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class gameManager : MonoBehaviour
{
public GameObject square;
public GameObject endPanel;
public Text timeTxt;
public Text thisScoreTxt;
public Text maxScoreTxt;
float alive = 0f;
bool isRunning = true;
public static gameManager I;
void Awake()
{
I = this;
}
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1.0f;
InvokeRepeating("makeSquare", 0.0f, 0.5f);
}
// Update is called once per frame
void Update()
{
if (isRunning)
{
alive += Time.deltaTime;
timeTxt.text = alive.ToString("N2");
}
}
void makeSquare()
{
Instantiate(square);
}
public void gameOver()
{
isRunning = false;
Time.timeScale = 0.0f;
thisScoreTxt.text = alive.ToString("N2");
endPanel.SetActive(true);
if (PlayerPrefs.HasKey("bestScore") == false)
{
PlayerPrefs.SetFloat("bestScore", alive);
}
else
{
if (PlayerPrefs.GetFloat("bestScore") < alive)
{
PlayerPrefs.SetFloat("bestScore", alive);
}
}
float maxScore = PlayerPrefs.GetFloat("bestScore");
maxScoreTxt.text = maxScore.ToString("N2");
}
public void retry()
{
SceneManager.LoadScene("02_mid");
}
}
// 2주차 강의자료 대로 코드를 작성한 예시입니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class gameManager : MonoBehaviour
{
public GameObject square;
public GameObject endPanel;
public Text timeTxt;
public Text thisScoreTxt;
public Text maxScoreTxt;
public Text bestScoreTxt;
float alive = 0f;
bool isRunning = true;
public static gameManager I;
void Awake()
{
I = this;
}
// Start is called before the first frame update
void Start()
{
Time.timeScale = 1.0f;
InvokeRepeating("makeSquare", 0.0f, 0.5f);
}
// Update is called once per frame
void Update()
{
if (isRunning)
{
alive += Time.deltaTime;
timeTxt.text = alive.ToString("N2");
}
}
void makeSquare()
{
Instantiate(square);
}
public void gameOver()
{
isRunning = false;
Time.timeScale = 0.0f;
thisScoreTxt.text = alive.ToString("N2");
endPanel.SetActive(true);
if (PlayerPrefs.HasKey("bestScore") == false)
{
PlayerPrefs.SetFloat("bestScore", alive);
}
else
{
if (PlayerPrefs.GetFloat("bestScore") < alive)
{
PlayerPrefs.SetFloat("bestScore", alive);
}
}
bestScoreTxt.text = PlayerPrefs.GetFloat("bestScore").ToString("N2");
}
public void retry()
{
SceneManager.LoadScene("02_mid");
}
}
