前提
ブロック崩しのクリア画面を作っています。
プレイシーンのスコアをクリア画面に反映させたいのですが、参考にしたコードではスコアが0にリセットされてしまいます。
実現したいこと
見よう見まねでやったので直すべきところが沢山あるかもしれませんがこんな感じにしたいです。
- プレイシーンのスコアはリセット時に0にしたい
- クリアシーンにスコアを反映させたい。
ご教授お願い致します。
発生している問題
ハイスコアは保持されているがスコアは0
クリアシーン
プレイシーン
スコアのテキストにアタッチしているコード
C#
1public class sceneScore : MonoBehaviour { 2 3 // スコアを表示する 4 public Text ScoreText; 5 int score; 6 7 void Start () 8 { 9 score = Score.getscore(); 10 ScoreText.text = string.Format ("Score:{0}" , score); 11 12 } 13 14 15 16} 17
プレイシーンのスコア、ハイスコアのコード
C#
1public class Score : MonoBehaviour { 2 3 4 public Text scoreText; 5 6 public Text highScoreText; 7 8 public static int score = 0; 9 10 11 private int highScore; 12 13 14 15 private string highScoreKey = "highScore"; 16 17 public static int getscore() 18 { 19 return score; 20 } 21 22 void Start () 23 { 24 score =0; 25 Initialize (); 26 } 27 28 void Update () 29 { 30 31 if (highScore < score) { 32 highScore = score; 33 } 34 35 36 scoreText.text = score.ToString (); 37 highScoreText.text = highScore.ToString (); 38 } 39 40 41 private void Initialize () 42 { 43 44 45 46 47 highScore = PlayerPrefs.GetInt (highScoreKey, 0); 48 } 49 50 51 public void AddPoint (int point) 52 { 53 score = score + point; 54 } 55 56 57 public void Save () 58 { 59 60 PlayerPrefs.SetInt (highScoreKey, highScore); 61 PlayerPrefs.Save (); 62 63 64 65 66 67 Initialize (); 68 69 70 } 71}
クリア時のコード
C#
1 2public class GameClear : MonoBehaviour 3{ 4 5 Transform myTransform; 6 7 // Start is called before the first frame update 8 void Start() 9 { 10 myTransform = transform; 11 12 } 13 14 // Update is called once per frame 15 void Update() 16 { 17 if(myTransform.childCount == 0) 18 { 19 Time.timeScale = 0f; 20 SceneManager.LoadScene("GameClear"); 21 22 23 } 24 25 26 } 27 }
補足情報(FW/ツールのバージョンなど)
Unity 2021.3.4f1
Visual Studio Code

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。