質問編集履歴

3

追記です

2019/11/09 14:30

投稿

sancor
sancor

スコア5

test CHANGED
File without changes
test CHANGED
@@ -424,4 +424,4 @@
424
424
 
425
425
  CursorShotScript.Update () (at Assets/CursorShotScript.cs:24)
426
426
 
427
- です。
427
+ です。初期配置しているmatoPrefabを破壊したときに発生しました。

2

解凍をいただいての変更後の問題点を追加させていただきました

2019/11/09 14:30

投稿

sancor
sancor

スコア5

test CHANGED
File without changes
test CHANGED
@@ -313,3 +313,115 @@
313
313
  ```
314
314
 
315
315
  変な文章で申し訳ありません。
316
+
317
+ ###回答を受けての変更後
318
+
319
+ snowshinkさんの回答をいただいて変更したときのスクリプトとエラーです
320
+
321
+ 変更したスクリプトはUIDirectorとClearDirectorです
322
+
323
+ ```UIDirector
324
+
325
+ using System.Collections;
326
+
327
+ using System.Collections.Generic;
328
+
329
+ using UnityEngine;
330
+
331
+ using UnityEngine.UI;
332
+
333
+
334
+
335
+ public class UIDirector : MonoBehaviour {
336
+
337
+
338
+
339
+ public Text score;
340
+
341
+ public int score_current=0;
342
+
343
+ // Use this for initialization
344
+
345
+ void Start () {
346
+
347
+ score = GetComponent<Text>();
348
+
349
+ DontDestroyOnLoad(this.gameObject);
350
+
351
+ }
352
+
353
+ public void scoreincrease(){
354
+
355
+ this.score_current++;
356
+
357
+ this.score.text=score_current.ToString();
358
+
359
+ }
360
+
361
+ }
362
+
363
+ ```
364
+
365
+ ```ClearDirector
366
+
367
+ using System.Collections;
368
+
369
+ using System.Collections.Generic;
370
+
371
+ using UnityEngine;
372
+
373
+ using UnityEngine.SceneManagement;
374
+
375
+ using UnityEngine.UI;
376
+
377
+
378
+
379
+
380
+
381
+ public class ClearDirector : MonoBehaviour {
382
+
383
+ public GameObject score;
384
+
385
+ void Start ()
386
+
387
+ {
388
+
389
+ UIDirector loadScene = FindObjectOfType<UIDirector> ();
390
+
391
+ Debug.Log (loadScene.score);
392
+
393
+ }
394
+
395
+
396
+
397
+ // Update is called once per frame
398
+
399
+ void Update () {
400
+
401
+ if(Input.GetMouseButtonDown(0)){
402
+
403
+ SceneManager.LoadScene("Mainmenu");
404
+
405
+ }
406
+
407
+ }
408
+
409
+ }
410
+
411
+ ```
412
+
413
+ エラー画面はこんな感じです。
414
+
415
+ ![イメージ説明](f0782245e1e1f959d609157ecfbea917.jpeg)
416
+
417
+ エラー文章は、
418
+
419
+ NullReferenceException: Object reference not set to an instance of an object
420
+
421
+ UIDirector.scoreincrease () (at Assets/UIDirector.cs:17)
422
+
423
+ CursorShotScript.Shot () (at Assets/CursorShotScript.cs:40)
424
+
425
+ CursorShotScript.Update () (at Assets/CursorShotScript.cs:24)
426
+
427
+ です。

1

不足していた点があったので関係しそうなものをすべて貼り付けます。

2019/11/09 13:46

投稿

sancor
sancor

スコア5

test CHANGED
File without changes
test CHANGED
@@ -6,12 +6,118 @@
6
6
 
7
7
 
8
8
 
9
+ 流れとしては、ゲームシーンで、CursorShotScriptでRayがmatoPrefabを破壊しそのたびにUIDirectorのscoreincrease関数を起動します。
10
+
11
+ UIDirectorでscoreにmatoPrefabが破壊された回数を表示し、TimerScriptで制限時間が来たらクリアシーンでClearDirector内で最終的なスコアの数値をコンソールに表示するようにしたいです。
12
+
13
+
14
+
9
- まずゲームシーンスコアを表示させているスクリプトで
15
+ まずCursorShotScriptです
10
16
 
11
17
  ```c#
12
18
 
19
+ using UnityEngine;
20
+
13
21
  using System.Collections;
14
22
 
23
+ using UnityEngine.SceneManagement;
24
+
25
+
26
+
27
+ public class CursorShotScript : MonoBehaviour {
28
+
29
+
30
+
31
+ // カーソルに使用するテクスチャ
32
+
33
+ [SerializeField]
34
+
35
+ private Texture2D cursor;
36
+
37
+ [SerializeField]
38
+
39
+ private matoGenerator matoGenerator;
40
+
41
+ public GameObject matoPrefab;
42
+
43
+ GameObject gameobject;
44
+
45
+
46
+
47
+ void Start () {
48
+
49
+ // カーソルを自前のカーソルに変更
50
+
51
+ Cursor.SetCursor(cursor, new Vector2(cursor.width / 2, cursor.height / 2), CursorMode.ForceSoftware);
52
+
53
+ }
54
+
55
+
56
+
57
+ void Update () {
58
+
59
+ // マウスの左クリックで撃つ
60
+
61
+ if(Input.GetButtonDown("Fire1")) {
62
+
63
+ Shot();
64
+
65
+ }
66
+
67
+ }
68
+
69
+
70
+
71
+ // 敵を撃つ
72
+
73
+ void Shot() {
74
+
75
+ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
76
+
77
+
78
+
79
+ float maxDistance=10;
80
+
81
+
82
+
83
+ RaycastHit2D hit = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction, maxDistance, LayerMask.GetMask("Enemy"));
84
+
85
+
86
+
87
+ if(hit.collider) {
88
+
89
+ matoGenerator.Create();
90
+
91
+ Destroy(hit.collider.gameObject);
92
+
93
+ GameObject director = GameObject.Find("UIDirector");
94
+
95
+ director.GetComponent<UIDirector>().scoreincrease();
96
+
97
+ }
98
+
99
+ if(hit.collider.tag=="Enemy"){
100
+
101
+ SceneManager.LoadScene("LoseScene");
102
+
103
+ Debug.Log ("RayがPlayerに当たった");
104
+
105
+ }
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ ```
114
+
115
+ 次にUIDirectorです。
116
+
117
+ ```c#
118
+
119
+ using System.Collections;
120
+
15
121
  using System.Collections.Generic;
16
122
 
17
123
  using UnityEngine;
@@ -50,12 +156,116 @@
50
156
 
51
157
  ```
52
158
 
53
- 次にクリアシーンスコアを表示るようにしたいスクリプトです
159
+ 次にTimerScriptです
54
160
 
55
161
  ```c#
56
162
 
163
+ using UnityEngine;
164
+
57
165
  using System.Collections;
58
166
 
167
+ using UnityEngine.UI;
168
+
169
+ using UnityEngine.SceneManagement;
170
+
171
+
172
+
173
+ public class TimerScript : MonoBehaviour {
174
+
175
+
176
+
177
+ // トータル制限時間
178
+
179
+ private float totalTime;
180
+
181
+ // 制限時間(分)
182
+
183
+ [SerializeField]
184
+
185
+ private int minute;
186
+
187
+ // 制限時間(秒)
188
+
189
+ [SerializeField]
190
+
191
+ private float seconds;
192
+
193
+ // 前回Update時の秒数
194
+
195
+ private float oldSeconds;
196
+
197
+ private Text timerText;
198
+
199
+
200
+
201
+ void Start () {
202
+
203
+ totalTime = minute * 60 + seconds;
204
+
205
+ oldSeconds = 0f;
206
+
207
+ timerText = GetComponentInChildren<Text>();
208
+
209
+ }
210
+
211
+
212
+
213
+ void Update () {
214
+
215
+ // 制限時間が0秒以下なら何もしない
216
+
217
+ if (totalTime <= 0f) {
218
+
219
+ return;
220
+
221
+ }
222
+
223
+ // 一旦トータルの制限時間を計測;
224
+
225
+ totalTime = minute * 60 + seconds;
226
+
227
+ totalTime -= Time.deltaTime;
228
+
229
+
230
+
231
+ // 再設定
232
+
233
+ minute = (int) totalTime / 60;
234
+
235
+ seconds = totalTime - minute * 60;
236
+
237
+
238
+
239
+ // タイマー表示用UIテキストに時間を表示する
240
+
241
+ if((int)seconds != (int)oldSeconds) {
242
+
243
+ timerText.text = minute.ToString("00") + ":" + ((int) seconds).ToString("00");
244
+
245
+ }
246
+
247
+ oldSeconds = seconds;
248
+
249
+
250
+
251
+ if(totalTime <= 0f) {
252
+
253
+ SceneManager.LoadScene("ClearScene");
254
+
255
+ }
256
+
257
+ }
258
+
259
+ }
260
+
261
+ ```
262
+
263
+ 最後にClearDirectorです。
264
+
265
+ ```c#
266
+
267
+ using System.Collections;
268
+
59
269
  using System.Collections.Generic;
60
270
 
61
271
  using UnityEngine;
@@ -102,4 +312,4 @@
102
312
 
103
313
  ```
104
314
 
105
- よろくお願いいたし
315
+ 変な文章で申訳ありせん