質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

2回答

1355閲覧

Unity クリアシーンからの移行

torn

総合スコア5

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/12/17 07:42

クリアシーンからタイトルシーンへの移行はできたのですが、
そこからまたゲームプレイをすると
残機やスコアなどが前回からの引継ぎのままになっており、初期化されません。
どのようにすれば初期化されるでしょうか?

クリア画面のスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class Clear : MonoBehaviour 7{ 8 [Header("フェード")]public FadeImage fade; 9 [Header("ボタン時に鳴らすSE")]public AudioClip startSE; 10 11 private bool firstPush = false; 12 private bool goNextScene = false; 13 14 public void PressStart() 15 { 16 Debug.Log("Press Start!"); 17 18 if(!firstPush) 19 { 20 GManager.instance.PlaySE(startSE); 21 fade.StartFadeOut(); 22 firstPush = true; 23 } 24 } 25 26 private void Update() 27 { 28 if(!goNextScene && fade.IsFadeOutComplete()) 29 { 30 SceneManager.LoadScene("TitleScene"); 31 goNextScene = true; 32 } 33 } 34}

GManagerのスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GManager : MonoBehaviour 6{ 7 public static GManager instance = null; 8 9 [Header("スコア")]public int score; 10 [Header("現在のスコア")]public int stageNum; 11 [Header("現在の復帰位置")]public int continueNum; 12 [Header("現在の残機")]public int heartNum; 13 [Header("デフォルトの残機")]public int defaultHeartNum; 14 [HideInInspector]public bool isGameOver = false; 15 16 private AudioSource audioSource = null; 17 18 private void Awake() 19 { 20 if(instance == null) 21 { 22 instance = this; 23 DontDestroyOnLoad(this.gameObject); 24 } 25 else 26 { 27 Destroy(this.gameObject); 28 } 29 } 30 31 private void Start() 32 { 33 audioSource = GetComponent<AudioSource>(); 34 } 35 36 ///<summary> 37 ///残機を1つ増やす 38 ///</summary> 39 public void AddHeartNum() 40 { 41 if(heartNum < 99) 42 { 43 ++heartNum; 44 } 45 } 46 47 ///<summary> 48 ///残機を1つ減らす 49 ///</summary> 50 public void SubHeartNum() 51 { 52 if(heartNum > 0) 53 { 54 --heartNum; 55 } 56 else 57 { 58 isGameOver = true; 59 } 60 } 61 62 ///<summary> 63 ///最初から始める時の処理 64 ///</summary> 65 public void RetryGame() 66 { 67 isGameOver = false; 68 heartNum = defaultHeartNum; 69 score = 0; 70 stageNum = 1; 71 continueNum = 0; 72 } 73 74 ///<summary> 75 ///SEを鳴らす 76 ///</summary> 77 public void PlaySE(AudioClip clip) 78 { 79 if(audioSource != null) 80 { 81 audioSource.PlayOneShot(clip); 82 } 83 else 84 { 85 Debug.Log("オーディオソースが設定されていません"); 86 } 87 } 88}

タイトルシーンのスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5 6public class Title : MonoBehaviour 7{ 8 [Header("フェード")]public FadeImage fade; 9 [Header("ゲームスタート時に鳴らすSE")]public AudioClip startSE; 10 11 private bool firstPush = false; 12 private bool goNextScene = false; 13 14 15 public void PressStart() 16 { 17 Debug.Log("Press Start!"); 18 19 if(!firstPush) 20 { 21 GManager.instance.PlaySE(startSE); 22 fade.StartFadeOut(); 23 firstPush = true; 24 } 25 } 26 27 private void Update() 28 { 29 if(!goNextScene && fade.IsFadeOutComplete()) 30 { 31 SceneManager.LoadScene("stage1"); 32 goNextScene = true; 33 } 34 } 35} 36

スコアのスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Score : MonoBehaviour 7{ 8 private Text scoreText = null; 9 private int oldScore = 0; 10 11 void Start() 12 { 13 scoreText = GetComponent<Text>(); 14 if(GManager.instance != null) 15 { 16 scoreText.text = "Score " + GManager.instance.score; 17 } 18 else 19 { 20 Debug.Log("ゲームマネージャー置き忘れてるよ!"); 21 Destroy(this); 22 } 23 } 24 25 void Update() 26 { 27 if(oldScore != GManager.instance.score) 28 { 29 scoreText.text = "Score " + GManager.instance.score; 30 oldScore = GManager.instance.score; 31 } 32 } 33}

残機のスクリプト

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Heart : MonoBehaviour 7{ 8 private Text heartText = null; 9 private int oldHeartNum = 0; 10 11 void Start() 12 { 13 heartText = GetComponent<Text>(); 14 if(GManager.instance != null) 15 { 16 heartText.text = "× " + GManager.instance.heartNum; 17 } 18 else 19 { 20 Debug.Log("ゲームマネージャー置き忘れてるよ!"); 21 Destroy(this); 22 } 23 } 24 25 void Update() 26 { 27 if(oldHeartNum != GManager.instance.heartNum) 28 { 29 heartText.text = "× " + GManager.instance.heartNum; 30 oldHeartNum = GManager.instance.heartNum; 31 } 32 } 33}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

クリアスクリプトにGManager.instance.StartGame()をタイトルシーン遷移前に追加。
GManagerスクリプトに

C#

1public void StartGame() 2 { 3 isGameOver = false; 4 heartNum = defaultHeartNum; 5 score = 0; 6 stageNum = 1; 7 continueNum = 0; 8 }

を追加することでリセット完了。

投稿2020/12/17 08:31

torn

総合スコア5

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

GManagerがシングルトンで残ったままになってるので、明示的にリセットしない限りは引き継がれます。
たぶんRetryGameの処理を使うとデータがリセットされてますよね? なのでクリアしたあとの処理でリセットしなきゃいけない変数はリセットするような処理を入れてあげればよいかと思います

投稿2020/12/17 07:58

hogefugapiyo

総合スコア3302

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

torn

2020/12/17 08:03

RetryGameの処理を行うとデータはリセットされるようにできています。 クリアのスクリプトにRetryGameと全く同じ処理を入れればよいでしょうか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問