質問編集履歴

2

書式修正しました。

2020/07/09 12:18

投稿

marine08
marine08

スコア14

test CHANGED
File without changes
test CHANGED
@@ -6,17 +6,19 @@
6
6
 
7
7
  ②GameController:ヒエラルキー上のUICanvasに配置したTimerを表示・非表示等管理
8
8
 
9
+
10
+
11
+ *できていること
12
+
13
+ *再生ボタンを押し、ゲームクリアをするとTimerは表示され、Timerも動く。
14
+
9
15
   
10
16
 
11
17
  問題点とやりたいこと
12
18
 
13
- *再生ボタンを押し、ゲームクリアをするとTimerは表示されるのですが、Timerが動かないです。
19
+
14
-
20
+
15
- ゲームスタート時から動いていたTimerを、ゲーム終了時にストップさせ表示したい
21
+ TimerTextが表示された時からカウントが始まってしまうので、カウントはゲームスタート時から始まり、ゲームクリア時にカウントストップさせたい
16
-
17
-  今できていることは、TImerテキストの表示のみ。
18
-
19
-
20
22
 
21
23
 
22
24
 
@@ -40,63 +42,207 @@
40
42
 
41
43
  {
42
44
 
43
- private int minute;
44
-
45
- private float seconds;
46
-
47
- private float beforeSeconds;
48
-
49
- private Text timerText;
50
-
51
- public GameObject stageNextButton;
52
-
53
- float count;
54
-
55
- bool isCountUp = false;
56
-
57
- // Use this for initialization
58
-
59
-
60
-
61
- //カウントアップ
62
-
63
- private float countup = 0.0f;
64
-
65
-
66
-
67
- //時間を表示するText型の変数
68
-
69
- public Text timeText;
70
-
71
-
72
-
73
- public void Start()
74
-
75
- {
76
-
77
- ResetTimer();
78
-
79
- //時間をカウントする
80
-
81
- countup += Time.deltaTime;
82
-
83
- }
84
-
85
-   public void Update()
86
-
87
- {
45
+ using System.Collections;
46
+
47
+ using System.Collections.Generic;
48
+
49
+ using UnityEngine;
50
+
51
+ using UnityEngine.UI;
52
+
53
+ using UnityEngine.SceneManagement;
54
+
55
+ using System;
56
+
57
+
58
+
59
+ public class Timer : MonoBehaviour
60
+
61
+ {
62
+
63
+ private int minute;
64
+
65
+ private float seconds;
66
+
67
+ private float beforeSeconds;
68
+
69
+ private Text timerText;
70
+
71
+ public void Start()
72
+
73
+ {
74
+
75
+ minute = 0;
76
+
77
+ seconds = 0f;
78
+
79
+ beforeSeconds = 0f;
80
+
81
+ timerText = GetComponentInChildren<Text>();
82
+
83
+ DateTime now = DateTime.Now;
84
+
85
+ }
86
+
87
+
88
+
89
+ public void Update()
90
+
91
+ {
92
+
93
+
94
+
95
+ seconds += Time.deltaTime;
96
+
97
+ if (seconds >= 60f)
98
+
99
+ {
100
+
101
+ minute++;
102
+
103
+ seconds = seconds - 60;
104
+
105
+
106
+
107
+ }
108
+
109
+ // 値が変わった時テキストを更新
110
+
111
+ if ((int)seconds != (int)beforeSeconds)
112
+
113
+ {
114
+
115
+ timerText.text = minute.ToString("00") + ":" + ((int)seconds).ToString("00");
116
+
117
+ }
118
+
119
+ beforeSeconds = seconds;
120
+
121
+ }
122
+
123
+
124
+
125
+
126
+
127
+ }
128
+
129
+
130
+
131
+ ```
132
+
133
+
134
+
135
+ ```
136
+
137
+
138
+
139
+ public class GameController : MonoBehaviour
140
+
141
+ using System.Collections;
142
+
143
+ using System.Collections.Generic;
144
+
145
+ using UnityEngine;
146
+
147
+ using UnityEngine.UI;
148
+
149
+ using UnityEngine.SceneManagement;
150
+
151
+
152
+
153
+ public class GameController : MonoBehaviour
154
+
155
+ {
156
+
157
+ public Timer timer;
158
+
159
+ public GameObject TitleText;
160
+
161
+ public GameObject gameOverText;
162
+
163
+ public Text scoreText;
164
+
165
+ public GameObject Timer;
166
+
167
+ public GameObject GameCrearStageNextText;
168
+
169
+ public int score = 0;
170
+
171
+ // ステージ数のテキスト
172
+
173
+ public Text stageNumberText;
174
+
175
+   public GameObject stageNextButton;
176
+
177
+ public GameObject retryButton;
178
+
179
+ public GameObject Playership1;
180
+
181
+ public GameObject shootingenemy1Prefab;
182
+
183
+ public GameObject advancedEnemy1Prefab;
184
+
185
+ public GameObject Enemy3rdPrefab;
186
+
187
+
188
+
189
+ public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
190
+
191
+ {
192
+
193
+
194
+
195
+ gameOverText.SetActive(false);
196
+
197
+ stageNextButton.SetActive(false);
198
+
199
+ retryButton.SetActive(false);
200
+
201
+ GameCrearStageNextText.SetActive(false);
202
+
203
+ //timer = GameObject.Find("Timer").GetComponent<Timer>();
204
+
205
+ timer.Start();
206
+
207
+ timer.Update();
208
+
209
+ Timer.SetActive(false);
210
+
211
+ scoreText.text = "SCORE:" + score;
212
+
213
+
214
+
215
+ }
88
216
 
89
217
 
90
218
 
219
+ public void AddScore()  //Scoreを100ずつ増やす
220
+
221
+ {
222
+
223
+ score += 100;
224
+
225
+ scoreText.text = "SCORE:" + score;
226
+
227
+
228
+
91
- if (isCountUp)
229
+ if (score == 100)
92
230
 
93
231
  {
94
232
 
95
- //時間を表示する
233
+ stageNextButton.SetActive(true);
96
-
234
+
97
- timeText.text = countup.ToString("f1") + "秒";
235
+ GameCrearStageNextText.SetActive(true);
236
+
98
-
237
+ Timer.SetActive(true);
238
+
99
-
239
+ Destroy(Playership1);
240
+
241
+ Destroy(shootingenemy1Prefab);
242
+
243
+ Destroy(advancedEnemy1Prefab);
244
+
245
+ Destroy(Enemy3rdPrefab);
100
246
 
101
247
  }
102
248
 
@@ -104,110 +250,68 @@
104
250
 
105
251
 
106
252
 
107
- public void StartTimer()
253
+ public void GameOver()  //GameOver文字を表示・リトライボタンを出す
108
-
254
+
109
- {
255
+ {
256
+
110
-
257
+ gameOverText.SetActive(true);
258
+
111
- isCountUp = true;
259
+ retryButton.SetActive(true);
112
-
260
+
261
+
262
+
263
+
264
+
113
- }
265
+ }
114
-
115
-
116
-
266
+
267
+
268
+
117
- public void StopTimer()
269
+ public void RetryScene()   //Mainシーンをロード
118
-
270
+
119
- {
271
+ {
120
-
272
+
121
- isCountUp = false;
273
+ SceneManager.LoadScene("first");
122
-
274
+
275
+
276
+
123
- }
277
+ }
124
-
125
-
126
-
278
+
279
+
280
+
281
+
282
+
127
- public void ResetTimer()
283
+ public void FirstToSecondButton()
128
-
284
+
129
- {
285
+ {
130
-
286
+
131
- StopTimer();
287
+ SceneManager.LoadScene("Second");
132
-
133
- count = 0.0f;
288
+
134
-
135
- }
289
+ }
290
+
291
+
292
+
293
+ public void SecondToThirdButton()
294
+
295
+ {
296
+
297
+ SceneManager.LoadScene("Third");
298
+
299
+ }
300
+
301
+ public void ThirdTo4ThButton()
302
+
303
+ {
304
+
305
+ SceneManager.LoadScene("4th");
306
+
307
+ }
308
+
309
+
136
310
 
137
311
  }
138
312
 
313
+
314
+
315
+
316
+
139
317
  ```
140
-
141
-
142
-
143
- ```
144
-
145
-
146
-
147
- public class GameController : MonoBehaviour
148
-
149
- <略>
150
-
151
- public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
152
-
153
- {
154
-
155
-
156
-
157
- gameOverText.SetActive(false);
158
-
159
- stageNextButton.SetActive(false);
160
-
161
- retryButton.SetActive(false);
162
-
163
- GameCrearStageNextText.SetActive(false);
164
-
165
- timer = GameObject.Find("Timer").GetComponent<Timer>();
166
-
167
- timer.StartTimer();
168
-
169
- Timer.SetActive(false);
170
-
171
- scoreText.text = "SCORE:" + score;
172
-
173
-
174
-
175
- }
176
-
177
-
178
-
179
- public void AddScore()  //Scoreを100ずつ増やす
180
-
181
- {
182
-
183
- score += 100;
184
-
185
- scoreText.text = "SCORE:" + score;
186
-
187
-
188
-
189
- if (score == 1000)
190
-
191
- {
192
-
193
- stageNextButton.SetActive(true);
194
-
195
- GameCrearStageNextText.SetActive(true);
196
-
197
- timer.StopTimer();
198
-
199
- Timer.SetActive(true);
200
-
201
- Destroy(Playership1);
202
-
203
- Destroy(shootingenemy1Prefab);
204
-
205
- Destroy(advancedEnemy1Prefab);
206
-
207
- Destroy(Enemy3rdPrefab);
208
-
209
- }
210
-
211
- }
212
-
213
- ```

1

書式の改善を図りました。ご指摘通り、質問の内容が明確でなかったです。申し訳ありません!!

2020/07/09 12:18

投稿

marine08
marine08

スコア14

test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,28 @@
1
+ *やりたい事
2
+
1
- Timerのスクリプトと、ゲーム全体を管理するスクリプトで、ゲームスタートからゲームクリアまでの経過時間をリザルトとして表示たいのですがく行きません
3
+  unity2Dシューティングゲームにて、ゲームクリアしただけに、プレイに要した時間を表示させたい。こために二つのスクリプトを作りした
4
+
2
-
5
+ ①TimerScript:時間の計測はゲームスタート時START、ゲームクリア時にSTOPさせたい
6
+
7
+ ②GameController:ヒエラルキー上のUICanvasに配置したTimerを表示・非表示等管理
8
+
9
+  
10
+
11
+ 問題点とやりたいこと
12
+
13
+ *再生ボタンを押し、ゲームクリアをするとTimerは表示されるのですが、Timerが動かないです。
14
+
15
+ →ゲームスタート時から動いていたTimerを、ゲーム終了時にストップさせ表示したい
16
+
3
- アドバイスをただけると嬉しいです
17
+  今できているは、TImerテキストの表示のみ
4
-
5
-
6
-
18
+
19
+
20
+
21
+
22
+
23
+
24
+
7
- ①Timerのスクリプト
25
+ ```unity C#
8
26
 
9
27
  using System.Collections;
10
28
 
@@ -86,7 +104,7 @@
86
104
 
87
105
 
88
106
 
89
- public void StartTimer()
107
+ public void StartTimer()
90
108
 
91
109
  {
92
110
 
@@ -100,7 +118,7 @@
100
118
 
101
119
  {
102
120
 
103
-    isCountUp = false;
121
+ isCountUp = false;
104
122
 
105
123
  }
106
124
 
@@ -118,61 +136,19 @@
118
136
 
119
137
  }
120
138
 
121
-
122
-
123
- ②Game全体を管理するスクリプト
139
+ ```
124
-
125
- using System.Collections;
140
+
126
-
127
- using System.Collections.Generic;
141
+
128
-
142
+
129
- using UnityEngine;
143
+ ```
130
-
131
- using UnityEngine.UI;
132
-
133
- using UnityEngine.SceneManagement;
134
144
 
135
145
 
136
146
 
137
147
  public class GameController : MonoBehaviour
138
148
 
139
- {
140
-
141
- public Timer timer;
149
+ <略>
142
-
143
- public GameObject TitleText;
150
+
144
-
145
- public GameObject gameOverText;
146
-
147
- public Text scoreText;
148
-
149
- public GameObject Timer;
150
-
151
- public GameObject GameCrearStageNextText;
152
-
153
- public int score = 0;
154
-
155
- // ステージ数のテキスト
156
-
157
- public Text stageNumberText;
158
-
159
-
160
-
161
- public GameObject stageNextButton;
162
-
163
- public GameObject retryButton;
164
-
165
- public GameObject Playership1;
166
-
167
- public GameObject shootingenemy1Prefab;
168
-
169
- public GameObject advancedEnemy1Prefab;
170
-
171
- public GameObject Enemy3rdPrefab;
172
-
173
-
174
-
175
- public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
151
+ public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
176
152
 
177
153
  {
178
154
 
@@ -210,7 +186,7 @@
210
186
 
211
187
 
212
188
 
213
- if (score == 100)
189
+ if (score == 1000)
214
190
 
215
191
  {
216
192
 
@@ -234,54 +210,4 @@
234
210
 
235
211
  }
236
212
 
237
-
238
-
239
- public void GameOver()  //GameOver文字を表示・リトライボタンを出す
240
-
241
- {
213
+ ```
242
-
243
- gameOverText.SetActive(true);
244
-
245
- retryButton.SetActive(true);
246
-
247
-
248
-
249
-
250
-
251
- }
252
-
253
-
254
-
255
- public void RetryScene()   //Mainシーンをロード
256
-
257
- {
258
-
259
- SceneManager.LoadScene("first");
260
-
261
-
262
-
263
- }
264
-
265
-
266
-
267
-
268
-
269
- public void FirstToSecondButton()
270
-
271
- {
272
-
273
- SceneManager.LoadScene("Second");
274
-
275
- }
276
-
277
-
278
-
279
- public void SecondToThirdButton()
280
-
281
- {
282
-
283
- SceneManager.LoadScene("Third");
284
-
285
- }
286
-
287
- }