우여 곡절 끝에, 많은 버그를 만나고, 코드를 어떻게 구성할지 고민하다가.
씬 전환 시 데이터값을 어떻게 넘기는가에 대한 고민을 하게 되었다.
- GameScene에서 점수를 획득했는데 이것을 어떻게 다른 씬에서 이용할 수 있을까?
- 오브젝트를 남기는 것이 아닌, 값만 남기면 되는데 어떻게 할 수 있을까?
- 씬 전환 시에 오브젝트를 어떻게 유지할 것인가?
위와 같은 고민을 하게 되었고, DontDestroyOnload(Gameobject) 를 보고... 헷갈리기 시작했다..
별첨 1. 화면 겹침
해결
분명히 간단히 해결해서 글쓰겠지 하셨겠지만.. (4시간 삽질했습니다.. 구글링 해도 안나오고.. 코드로 오브젝트 구분하고.. 늅늅에겐 너무 힘든 과정)


대체 이거 설명해둔데가 없어.. 독학은.. 어디서 배워야 하는건가!
별첨 2. 씬 전환 시 데이터 값을 받아오는 코드
GameDirector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
GameObject hpGauge;
GameObject score;
public static int point; // static으로 전역으로 선언해서 다른 씬에서 참조시킴
void Start()
{
this.hpGauge = GameObject.Find("HPGauge");
this.score = GameObject.Find("Score");
point = 0; // ClearScene에서 넘어오면 값을 초기화 시켜줌
}
public void DecreaseHp()
{
this.hpGauge.GetComponent<Image>().fillAmount -= 0.1f;
}
public void IncreaseHp()
{
this.hpGauge.GetComponent<Image>().fillAmount += 0.2f;
}
private void Update()
{
if (this.hpGauge.GetComponent<Image>().fillAmount <= 0)
{
SceneManager.LoadScene("ClearScene");
Destroy(gameObject);
}
// 실시간으로 점수가 추가됨을 보여주기 위한
this.score.GetComponent<TMPro.TextMeshProUGUI>().text = "Score : " + GetPoint();
}
// 다른 스크립트에서 값을 변동을 주기 위해서 전역 함수로 선언한 모습
public static void SetPoint() => point += 1;
public static int GetPoint() => point;
}
ClearDirector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ClearDirector : MonoBehaviour
{
GameObject finalScore;
private void Start()
{
this.finalScore = GameObject.Find("FinalScore");
//GameObject director = GameObject.Find("GameDirector");
}
void Update()
{
// 싱글톤 GameDirector.point 로 static 값을 넣어줌
this.finalScore.GetComponent<TMPro.TextMeshProUGUI>().text = "Your Score : " + GameDirector.point.ToString();
if (Input.GetKeyUp(KeyCode.Space))
{
SceneManager.LoadScene("GameScene");
}
}
}
※ 기본적으로 게임 시작부터, 화살을 피하고, 플레이하는 방법까지는 구현이 되어, 단발성 게임으로서의 역할을 하기 시작했다. 이제, 게임성에 대한 것을 구현할 필요성이 있다.
※ 게임의 난이도가 높아지면서 플레이가 어렵고, 한국인일 수록 어려운 난이도와 그에 따른 성취감의 요소가 필요하기 때문에, 게임성 요소에 대해 생각해 보도록 한다.
'개발 > 유니티(UNITY)' 카테고리의 다른 글
5-6 유니티로 화살 피하기 게임 만들기(심화2) (0) | 2023.10.30 |
---|---|
5-5 유니티로 화살 피하기 게임 만들기(심화1) (0) | 2023.10.29 |
5-4 유니티로 화살 피하기 게임 만들기 (0) | 2023.10.28 |
5-3 유니티로 화살 피하기 게임 만들기 (0) | 2023.10.27 |
5-2 유니티로 화살 피하기 게임 만들기 (0) | 2023.10.26 |