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

質問編集履歴

1

スクリプトの表示

2021/02/20 06:21

投稿

banana.red
banana.red

スコア4

title CHANGED
File without changes
body CHANGED
@@ -1,10 +1,84 @@
1
1
  ``````
2
2
  コードscore.cs
3
+ using System.Collections;
4
+ using System.Collections.Generic;
5
+ using UnityEngine;
6
+ using UnityEngine.UI;
7
+
8
+ public class Score : MonoBehaviour
9
+ {
10
+ public Text scoreText;
11
+ public Text highScoreText;
12
+ private int score;
13
+ private int highScore;
14
+ private string highScoreKey = "highScore";
15
+
16
+ void Start()
17
+ {
18
+ Initialize();
19
+ }
20
+
21
+ void Update()
22
+ {
23
+
24
+ if (highScore < score)
25
+ {
26
+ highScore = score;
27
+ }
28
+
29
+ scoreText.text = score.ToString();
30
+ highScoreText.text = highScore.ToString();
31
+ }
32
+
33
+ private void Initialize()
34
+ {
35
+ score = 0;
3
- ![イメージ説明](b89a249eee43ecb3de439922e5c03c68.png)
36
+ highScore = PlayerPrefs.GetInt(highScoreKey, 0);
37
+ }
38
+
39
+ public void AddPoint(int point)
40
+ {
41
+ score = score + point;
42
+ }
43
+
44
+
45
+ public void Save()
46
+ {
47
+
4
- ![イメージ説明](54a7e24c66ec357cdee44c5aa2584d85.png)
48
+ PlayerPrefs.SetInt(highScoreKey, highScore);
5
- ![イメージ説明](a1291b80c43447dd56377634e6c39a20.png)
49
+ PlayerPrefs.Save();
50
+
51
+
52
+ Initialize();
53
+ }
54
+ }
55
+
6
56
  コードblock.cs
57
+ public class Block : MonoBehaviour
58
+ {
59
+ // Start is called before the first frame update
60
+ void Start()
61
+ {
62
+
63
+ }
64
+
65
+ // Update is called once per frame
66
+ void Update()
67
+ {
68
+
69
+ }
7
- ![イメージ説明](e5bcc34943897d33476ce7da671108c7.png)
70
+ private void OnCollisionEnter(Collision collision)
71
+ {
72
+ Destroy(this.gameObject);
73
+ /*if (collision.gameObject.tag == "Ball")
74
+ {
75
+
76
+ FindObjectOfType<Score>().AddPoint(10);
77
+ Destroy(this.gameObject);
78
+
79
+ }*/
80
+ }
81
+ }
8
82
  ```### 前提・実現したいこと
9
83
 
10
84
  ここに質問の内容を詳しく書いてください。
@@ -28,4 +102,4 @@
28
102
 
29
103
  ### 補足情報(FW/ツールのバージョンなど)
30
104
 
31
- ここにより詳細情報を記載てください
105
+ block.csのif文は動かかったのでコメントアウトました