커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
2-10 최고기록을 나타내는 숫자가 변동되지 않습니다.
게임개발 종합반 v8
2주차
북마크
신*지
댓글
3
추천
0
조회수
20
조회수
20
답변 완료


안녕하세요~ 2-10 강의를 수강 중 오류가 발생하여 질문드립니다.

제일 처음에는 강의 영상대로 코드를 그대로 작성하여 gameManager에 넣었지만, 정상적으로 작동하지 않았습니다.

그 다음에는 강의 자료에 적혀있는대로 코드를 넣어보았지만, 이번에도 최고 점수를 나타내는 숫자가 변동되지 않아 강의를 끝마칠 수 없는 상황입니다.

어떻게 해야 최고점수에 해당하는 숫자를 변동시킬 수 있을까요?

두 코드 모두 NullReferenceException 오류가 발생합니다!


2-10 강의: https://online.spartacodingclub.kr/enrolleds/6524dd6d65ed5610a864e597/edetails/6524dd6e65ed5610a864e5ad?course_id=6083eaca6305e019d3e0c3b4

강의자료 본문: 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");
    }
}



취소
 공유
취소
댓글 0
댓글 알림
나의얼굴