質問編集履歴
3
追記です
title
CHANGED
File without changes
|
body
CHANGED
@@ -211,4 +211,4 @@
|
|
211
211
|
UIDirector.scoreincrease () (at Assets/UIDirector.cs:17)
|
212
212
|
CursorShotScript.Shot () (at Assets/CursorShotScript.cs:40)
|
213
213
|
CursorShotScript.Update () (at Assets/CursorShotScript.cs:24)
|
214
|
-
です。
|
214
|
+
です。初期配置しているmatoPrefabを破壊したときに発生しました。
|
2
解凍をいただいての変更後の問題点を追加させていただきました
title
CHANGED
File without changes
|
body
CHANGED
@@ -155,4 +155,60 @@
|
|
155
155
|
}
|
156
156
|
|
157
157
|
```
|
158
|
-
変な文章で申し訳ありません。
|
158
|
+
変な文章で申し訳ありません。
|
159
|
+
###回答を受けての変更後
|
160
|
+
snowshinkさんの回答をいただいて変更したときのスクリプトとエラーです
|
161
|
+
変更したスクリプトはUIDirectorとClearDirectorです
|
162
|
+
```UIDirector
|
163
|
+
using System.Collections;
|
164
|
+
using System.Collections.Generic;
|
165
|
+
using UnityEngine;
|
166
|
+
using UnityEngine.UI;
|
167
|
+
|
168
|
+
public class UIDirector : MonoBehaviour {
|
169
|
+
|
170
|
+
public Text score;
|
171
|
+
public int score_current=0;
|
172
|
+
// Use this for initialization
|
173
|
+
void Start () {
|
174
|
+
score = GetComponent<Text>();
|
175
|
+
DontDestroyOnLoad(this.gameObject);
|
176
|
+
}
|
177
|
+
public void scoreincrease(){
|
178
|
+
this.score_current++;
|
179
|
+
this.score.text=score_current.ToString();
|
180
|
+
}
|
181
|
+
}
|
182
|
+
```
|
183
|
+
```ClearDirector
|
184
|
+
using System.Collections;
|
185
|
+
using System.Collections.Generic;
|
186
|
+
using UnityEngine;
|
187
|
+
using UnityEngine.SceneManagement;
|
188
|
+
using UnityEngine.UI;
|
189
|
+
|
190
|
+
|
191
|
+
public class ClearDirector : MonoBehaviour {
|
192
|
+
public GameObject score;
|
193
|
+
void Start ()
|
194
|
+
{
|
195
|
+
UIDirector loadScene = FindObjectOfType<UIDirector> ();
|
196
|
+
Debug.Log (loadScene.score);
|
197
|
+
}
|
198
|
+
|
199
|
+
// Update is called once per frame
|
200
|
+
void Update () {
|
201
|
+
if(Input.GetMouseButtonDown(0)){
|
202
|
+
SceneManager.LoadScene("Mainmenu");
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
```
|
207
|
+
エラー画面はこんな感じです。
|
208
|
+

|
209
|
+
エラー文章は、
|
210
|
+
NullReferenceException: Object reference not set to an instance of an object
|
211
|
+
UIDirector.scoreincrease () (at Assets/UIDirector.cs:17)
|
212
|
+
CursorShotScript.Shot () (at Assets/CursorShotScript.cs:40)
|
213
|
+
CursorShotScript.Update () (at Assets/CursorShotScript.cs:24)
|
214
|
+
です。
|
1
不足していた点があったので関係しそうなものをすべて貼り付けます。
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,9 +2,62 @@
|
|
2
2
|
ネットに書いてあることも試してみたのですが、やり方が悪いのか上手くいきませんでした。
|
3
3
|
教えてほしいです。
|
4
4
|
|
5
|
+
流れとしては、ゲームシーンで、CursorShotScriptでRayがmatoPrefabを破壊しそのたびにUIDirectorのscoreincrease関数を起動します。
|
6
|
+
UIDirectorでscoreにmatoPrefabが破壊された回数を表示し、TimerScriptで制限時間が来たらクリアシーンでClearDirector内で最終的なスコアの数値をコンソールに表示するようにしたいです。
|
7
|
+
|
5
|
-
まず
|
8
|
+
まずCursorShotScriptです。
|
6
9
|
```c#
|
10
|
+
using UnityEngine;
|
7
11
|
using System.Collections;
|
12
|
+
using UnityEngine.SceneManagement;
|
13
|
+
|
14
|
+
public class CursorShotScript : MonoBehaviour {
|
15
|
+
|
16
|
+
// カーソルに使用するテクスチャ
|
17
|
+
[SerializeField]
|
18
|
+
private Texture2D cursor;
|
19
|
+
[SerializeField]
|
20
|
+
private matoGenerator matoGenerator;
|
21
|
+
public GameObject matoPrefab;
|
22
|
+
GameObject gameobject;
|
23
|
+
|
24
|
+
void Start () {
|
25
|
+
// カーソルを自前のカーソルに変更
|
26
|
+
Cursor.SetCursor(cursor, new Vector2(cursor.width / 2, cursor.height / 2), CursorMode.ForceSoftware);
|
27
|
+
}
|
28
|
+
|
29
|
+
void Update () {
|
30
|
+
// マウスの左クリックで撃つ
|
31
|
+
if(Input.GetButtonDown("Fire1")) {
|
32
|
+
Shot();
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
// 敵を撃つ
|
37
|
+
void Shot() {
|
38
|
+
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
39
|
+
|
40
|
+
float maxDistance=10;
|
41
|
+
|
42
|
+
RaycastHit2D hit = Physics2D.Raycast((Vector2)ray.origin, (Vector2)ray.direction, maxDistance, LayerMask.GetMask("Enemy"));
|
43
|
+
|
44
|
+
if(hit.collider) {
|
45
|
+
matoGenerator.Create();
|
46
|
+
Destroy(hit.collider.gameObject);
|
47
|
+
GameObject director = GameObject.Find("UIDirector");
|
48
|
+
director.GetComponent<UIDirector>().scoreincrease();
|
49
|
+
}
|
50
|
+
if(hit.collider.tag=="Enemy"){
|
51
|
+
SceneManager.LoadScene("LoseScene");
|
52
|
+
Debug.Log ("RayがPlayerに当たった");
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
```
|
58
|
+
次にUIDirectorです。
|
59
|
+
```c#
|
60
|
+
using System.Collections;
|
8
61
|
using System.Collections.Generic;
|
9
62
|
using UnityEngine;
|
10
63
|
using UnityEngine.UI;
|
@@ -24,9 +77,61 @@
|
|
24
77
|
}
|
25
78
|
|
26
79
|
```
|
27
|
-
次に
|
80
|
+
次にTimerScriptです。
|
28
81
|
```c#
|
82
|
+
using UnityEngine;
|
29
83
|
using System.Collections;
|
84
|
+
using UnityEngine.UI;
|
85
|
+
using UnityEngine.SceneManagement;
|
86
|
+
|
87
|
+
public class TimerScript : MonoBehaviour {
|
88
|
+
|
89
|
+
// トータル制限時間
|
90
|
+
private float totalTime;
|
91
|
+
// 制限時間(分)
|
92
|
+
[SerializeField]
|
93
|
+
private int minute;
|
94
|
+
// 制限時間(秒)
|
95
|
+
[SerializeField]
|
96
|
+
private float seconds;
|
97
|
+
// 前回Update時の秒数
|
98
|
+
private float oldSeconds;
|
99
|
+
private Text timerText;
|
100
|
+
|
101
|
+
void Start () {
|
102
|
+
totalTime = minute * 60 + seconds;
|
103
|
+
oldSeconds = 0f;
|
104
|
+
timerText = GetComponentInChildren<Text>();
|
105
|
+
}
|
106
|
+
|
107
|
+
void Update () {
|
108
|
+
// 制限時間が0秒以下なら何もしない
|
109
|
+
if (totalTime <= 0f) {
|
110
|
+
return;
|
111
|
+
}
|
112
|
+
// 一旦トータルの制限時間を計測;
|
113
|
+
totalTime = minute * 60 + seconds;
|
114
|
+
totalTime -= Time.deltaTime;
|
115
|
+
|
116
|
+
// 再設定
|
117
|
+
minute = (int) totalTime / 60;
|
118
|
+
seconds = totalTime - minute * 60;
|
119
|
+
|
120
|
+
// タイマー表示用UIテキストに時間を表示する
|
121
|
+
if((int)seconds != (int)oldSeconds) {
|
122
|
+
timerText.text = minute.ToString("00") + ":" + ((int) seconds).ToString("00");
|
123
|
+
}
|
124
|
+
oldSeconds = seconds;
|
125
|
+
|
126
|
+
if(totalTime <= 0f) {
|
127
|
+
SceneManager.LoadScene("ClearScene");
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
131
|
+
```
|
132
|
+
最後にClearDirectorです。
|
133
|
+
```c#
|
134
|
+
using System.Collections;
|
30
135
|
using System.Collections.Generic;
|
31
136
|
using UnityEngine;
|
32
137
|
using UnityEngine.SceneManagement;
|
@@ -50,4 +155,4 @@
|
|
50
155
|
}
|
51
156
|
|
52
157
|
```
|
53
|
-
|
158
|
+
変な文章で申し訳ありません。
|