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

質問編集履歴

2

書式修正しました。

2020/07/09 12:18

投稿

marine08
marine08

スコア14

title CHANGED
File without changes
body CHANGED
@@ -2,12 +2,13 @@
2
2
   unity2Dシューティングゲームにて、ゲームをクリアした時だけに、プレイに要した時間を表示させたい。このために、二つのスクリプトを作りました。
3
3
  ①TimerScript:時間の計測はゲームスタート時START、ゲームクリア時にSTOPさせたい
4
4
  ②GameController:ヒエラルキー上のUICanvasに配置したTimerを表示・非表示等管理
5
+
6
+ *できていること
7
+ *再生ボタンを押し、ゲームクリアをするとTimerは表示され、Timerも動く。
5
8
   
6
9
  問題点とやりたいこと
7
- *再生ボタンを押し、ゲームクリアをするとTimerは表示されるのですが、Timerが動かないです。
8
- →ゲームスタート時から動いていたTimerを、ゲーム終了時にストップさせ表示したい
9
-  今できていることは、TImerテキストの表示のみ。
10
10
 
11
+ TimerTextが表示された時からカウントが始まってしまうので、カウントはゲームスタート時から始まり、ゲームクリア時にカウントストップさせたい。
11
12
 
12
13
 
13
14
  ```unity C#
@@ -19,69 +20,88 @@
19
20
 
20
21
  public class Timer : MonoBehaviour
21
22
  {
22
- private int minute;
23
- private float seconds;
24
- private float beforeSeconds;
23
+ using System.Collections;
24
+ using System.Collections.Generic;
25
+ using UnityEngine;
25
- private Text timerText;
26
+ using UnityEngine.UI;
26
- public GameObject stageNextButton;
27
+ using UnityEngine.SceneManagement;
27
- float count;
28
+ using System;
28
- bool isCountUp = false;
29
- // Use this for initialization
30
29
 
30
+ public class Timer : MonoBehaviour
31
+ {
31
- //カウントアップ
32
+ private int minute;
33
+ private float seconds;
32
- private float countup = 0.0f;
34
+ private float beforeSeconds;
35
+ private Text timerText;
36
+ public void Start()
37
+ {
38
+ minute = 0;
39
+ seconds = 0f;
40
+ beforeSeconds = 0f;
41
+ timerText = GetComponentInChildren<Text>();
42
+ DateTime now = DateTime.Now;
43
+ }
33
44
 
34
- //時間を表示するText型の変数
35
- public Text timeText;
45
+ public void Update()
46
+ {
36
47
 
48
+ seconds += Time.deltaTime;
37
- public void Start()
49
+ if (seconds >= 60f)
38
- {
50
+ {
39
- ResetTimer();
51
+ minute++;
40
- //時間をカウントする
41
- countup += Time.deltaTime;
52
+ seconds = seconds - 60;
42
- }
43
-   public void Update()
44
- {
45
-
46
- if (isCountUp)
47
- {
48
- //時間を表示する
49
- timeText.text = countup.ToString("f1") + "秒";
50
53
 
51
- }
54
+ }
55
+ // 値が変わった時テキストを更新
56
+ if ((int)seconds != (int)beforeSeconds)
57
+ {
58
+ timerText.text = minute.ToString("00") + ":" + ((int)seconds).ToString("00");
52
- }
59
+ }
60
+ beforeSeconds = seconds;
61
+ }
53
62
 
54
- public void StartTimer()
55
- {
56
- isCountUp = true;
57
- }
58
63
 
59
- public void StopTimer()
60
- {
61
- isCountUp = false;
62
- }
64
+ }
63
65
 
64
- public void ResetTimer()
65
- {
66
- StopTimer();
67
- count = 0.0f;
68
- }
69
- }
70
66
  ```
71
67
 
72
68
  ```
73
69
 
74
70
  public class GameController : MonoBehaviour
71
+ using System.Collections;
72
+ using System.Collections.Generic;
73
+ using UnityEngine;
74
+ using UnityEngine.UI;
75
+ using UnityEngine.SceneManagement;
76
+
77
+ public class GameController : MonoBehaviour
78
+ {
75
- <略>
79
+ public Timer timer;
80
+ public GameObject TitleText;
81
+ public GameObject gameOverText;
82
+ public Text scoreText;
83
+ public GameObject Timer;
84
+ public GameObject GameCrearStageNextText;
85
+ public int score = 0;
86
+ // ステージ数のテキスト
87
+ public Text stageNumberText;
88
+   public GameObject stageNextButton;
89
+ public GameObject retryButton;
90
+ public GameObject Playership1;
91
+ public GameObject shootingenemy1Prefab;
92
+ public GameObject advancedEnemy1Prefab;
93
+ public GameObject Enemy3rdPrefab;
94
+
76
- public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
95
+ public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
77
96
  {
78
97
 
79
98
  gameOverText.SetActive(false);
80
99
  stageNextButton.SetActive(false);
81
100
  retryButton.SetActive(false);
82
101
  GameCrearStageNextText.SetActive(false);
83
- timer = GameObject.Find("Timer").GetComponent<Timer>();
102
+ //timer = GameObject.Find("Timer").GetComponent<Timer>();
84
- timer.StartTimer();
103
+ timer.Start();
104
+ timer.Update();
85
105
  Timer.SetActive(false);
86
106
  scoreText.text = "SCORE:" + score;
87
107
 
@@ -92,11 +112,10 @@
92
112
  score += 100;
93
113
  scoreText.text = "SCORE:" + score;
94
114
 
95
- if (score == 1000)
115
+ if (score == 100)
96
116
  {
97
117
  stageNextButton.SetActive(true);
98
118
  GameCrearStageNextText.SetActive(true);
99
- timer.StopTimer();
100
119
  Timer.SetActive(true);
101
120
  Destroy(Playership1);
102
121
  Destroy(shootingenemy1Prefab);
@@ -104,4 +123,37 @@
104
123
  Destroy(Enemy3rdPrefab);
105
124
  }
106
125
  }
126
+
127
+ public void GameOver()  //GameOver文字を表示・リトライボタンを出す
128
+ {
129
+ gameOverText.SetActive(true);
130
+ retryButton.SetActive(true);
131
+
132
+
133
+ }
134
+
135
+ public void RetryScene()   //Mainシーンをロード
136
+ {
137
+ SceneManager.LoadScene("first");
138
+
139
+ }
140
+
141
+
142
+ public void FirstToSecondButton()
143
+ {
144
+ SceneManager.LoadScene("Second");
145
+ }
146
+
147
+ public void SecondToThirdButton()
148
+ {
149
+ SceneManager.LoadScene("Third");
150
+ }
151
+ public void ThirdTo4ThButton()
152
+ {
153
+ SceneManager.LoadScene("4th");
154
+ }
155
+
156
+ }
157
+
158
+
107
159
  ```

1

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

2020/07/09 12:18

投稿

marine08
marine08

スコア14

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,16 @@
1
+ *やりたい事
1
- Timerのスクリプトと、ゲーム全体を管理するスクリプトで、ゲームスタートからゲームクリアまでの経過時間をリザルトとして表示たいのですがく行きません
2
+  unity2Dシューティングゲームにて、ゲームクリアしただけに、プレイに要した時間を表示させたい。こために二つのスクリプトを作りした
3
+ ①TimerScript:時間の計測はゲームスタート時START、ゲームクリア時にSTOPさせたい
4
+ ②GameController:ヒエラルキー上のUICanvasに配置したTimerを表示・非表示等管理
5
+  
6
+ 問題点とやりたいこと
7
+ *再生ボタンを押し、ゲームクリアをするとTimerは表示されるのですが、Timerが動かないです。
8
+ →ゲームスタート時から動いていたTimerを、ゲーム終了時にストップさせ表示したい
2
- アドバイスをただけると嬉しいです
9
+  今できているは、TImerテキストの表示のみ
3
10
 
11
+
12
+
4
- ①Timerのスクリプト
13
+ ```unity C#
5
14
  using System.Collections;
6
15
  using System.Collections.Generic;
7
16
  using UnityEngine;
@@ -42,14 +51,14 @@
42
51
  }
43
52
  }
44
53
 
45
- public void StartTimer()
54
+ public void StartTimer()
46
55
  {
47
56
  isCountUp = true;
48
57
  }
49
58
 
50
59
  public void StopTimer()
51
60
  {
52
-    isCountUp = false;
61
+ isCountUp = false;
53
62
  }
54
63
 
55
64
  public void ResetTimer()
@@ -58,34 +67,13 @@
58
67
  count = 0.0f;
59
68
  }
60
69
  }
70
+ ```
61
71
 
62
- ②Game全体を管理するスクリプト
72
+ ```
63
- using System.Collections;
64
- using System.Collections.Generic;
65
- using UnityEngine;
66
- using UnityEngine.UI;
67
- using UnityEngine.SceneManagement;
68
73
 
69
74
  public class GameController : MonoBehaviour
70
- {
71
- public Timer timer;
75
+ <略>
72
- public GameObject TitleText;
73
- public GameObject gameOverText;
74
- public Text scoreText;
75
- public GameObject Timer;
76
- public GameObject GameCrearStageNextText;
77
- public int score = 0;
78
- // ステージ数のテキスト
79
- public Text stageNumberText;
80
-
81
- public GameObject stageNextButton;
82
- public GameObject retryButton;
83
- public GameObject Playership1;
84
- public GameObject shootingenemy1Prefab;
85
- public GameObject advancedEnemy1Prefab;
86
- public GameObject Enemy3rdPrefab;
87
-
88
- public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
76
+ public void Start() //ゲームオーバーテキストは消えてる・スコアテキストの表示・ハイドボタン(stagenextbuttonは消す・リトライ消す)
89
77
  {
90
78
 
91
79
  gameOverText.SetActive(false);
@@ -104,7 +92,7 @@
104
92
  score += 100;
105
93
  scoreText.text = "SCORE:" + score;
106
94
 
107
- if (score == 100)
95
+ if (score == 1000)
108
96
  {
109
97
  stageNextButton.SetActive(true);
110
98
  GameCrearStageNextText.SetActive(true);
@@ -116,29 +104,4 @@
116
104
  Destroy(Enemy3rdPrefab);
117
105
  }
118
106
  }
119
-
120
- public void GameOver()  //GameOver文字を表示・リトライボタンを出す
121
- {
107
+ ```
122
- gameOverText.SetActive(true);
123
- retryButton.SetActive(true);
124
-
125
-
126
- }
127
-
128
- public void RetryScene()   //Mainシーンをロード
129
- {
130
- SceneManager.LoadScene("first");
131
-
132
- }
133
-
134
-
135
- public void FirstToSecondButton()
136
- {
137
- SceneManager.LoadScene("Second");
138
- }
139
-
140
- public void SecondToThirdButton()
141
- {
142
- SceneManager.LoadScene("Third");
143
- }
144
- }