teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

わかりやすく

2019/11/09 02:18

投稿

snowshink
snowshink

スコア140

answer CHANGED
@@ -2,5 +2,30 @@
2
2
  ゲームシーンがあって、ここにスコアオブジェクトがあって、
3
3
  クリアしたらゲームクリアシーンを読み込んでというのであれば、
4
4
  スコアオブジェクトをシーン読み込みで破棄させないようにしたらどうでしょうか。
5
+ ```Unity
6
+ Start(){
7
+ score = GameObject.Find("score");
5
- `DontDestoyOnLoad(score);`
8
+ DontDestoyOnLoad(score);
9
+ }
10
+ ```
6
- これで結果が保持され続けるのではないでしょうか。
11
+ これで結果が保持され続けるのではないでしょうか。
12
+
13
+ 気になったのですが、スコア表示するテキストオブジェクトとスコアを保持するオブジェクトを別々にする
14
+ 必要はないのでは。
15
+
16
+ ```Unity
17
+ Text score;
18
+ public int score_current = 0;
19
+
20
+ Start(){
21
+ score = GetComponent<Text>();
22
+ DontDestoyOnLoad(this.gameObject);
23
+ }I
24
+
25
+ public void IncreaseScore(){
26
+ score_current++;
27
+ score.text = score_current;
28
+ }
29
+ ```
30
+ こう変更してscoreにはめて?ください。
31
+ これで`SceneManager.LoadScene("GameClearScene");`しても結果は表示され続けます。