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

質問編集履歴

3

画像の追加

2017/09/16 18:08

投稿

sumikko6210
sumikko6210

スコア138

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,5 @@
1
1
  ###前提・実現したいこと
2
-
2
+ ![イメージ説明](cf744b8418294c22be42b07dbc389c26.png)
3
3
  Unity2Dのパズルをリセットするためにリセットボタンを設置して、そこにReset.csを紐づけて実行したところ、以下のエラーメッセージが発生しました。
4
4
 
5
5
  ###発生している問題・エラーメッセージ

2

コードの追加

2017/09/16 18:08

投稿

sumikko6210
sumikko6210

スコア138

title CHANGED
File without changes
body CHANGED
@@ -153,6 +153,167 @@
153
153
 
154
154
  ```
155
155
 
156
+ Group.cs
157
+ ```C#
158
+ using System.Collections;
159
+ using System.Collections.Generic;
160
+ using UnityEngine;
161
+
162
+ public class Group : MonoBehaviour
163
+ {
164
+ private Vector3 screenPoint;
165
+ private Vector3 offset;
166
+ private Count count;
167
+
168
+
169
+ //座標が範囲内であるか,Gridに値があるかどうかを判定
170
+ public bool isValidGridPos()
171
+ {
172
+ foreach (Transform child in transform)
173
+ {
174
+ Vector2 v = Grid.roundVec2(child.position);
175
+
176
+ if (!Grid.insideBorder(v))
177
+ return false;
178
+
179
+ if (Grid.grid[(int)v.x, (int)v.y] != null &&
180
+ Grid.grid[(int)v.x, (int)v.y].parent != transform)
181
+ return false;
182
+ }
183
+ return true;
184
+ }
185
+
186
+ void updateGrid()
187
+ {
188
+ for (int y = 0; y < Grid.h; ++y)
189
+ for (int x = 0; x < Grid.w; ++x)
190
+ if (Grid.grid[x, y] != null)
191
+ if (Grid.grid[x, y].parent == transform)
192
+ Grid.grid[x, y] = null;
193
+
194
+ foreach (Transform child in transform)
195
+ {
196
+ Vector2 v = Grid.roundVec2(child.position);
197
+ Grid.grid[(int)v.x, (int)v.y] = child;
198
+ }
199
+ }
200
+
201
+ //オブジェクトをクリックする
202
+ void OnMouseDown()
203
+ {
204
+ // マウスカーソルは、スクリーン座標なので、
205
+ // 対象のオブジェクトもスクリーン座標に変換してから計算する。
206
+
207
+ // このオブジェクトの位置(transform.position)をスクリーン座標に変換。
208
+ screenPoint = Camera.main.WorldToScreenPoint(transform.position);
209
+ // ワールド座標上の、マウスカーソルと、対象の位置の差分。
210
+ offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
211
+
212
+
213
+ }
214
+
215
+ //オブジェクトをドラッグする
216
+ void OnMouseDrag()
217
+ {
218
+ Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
219
+ Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset;
220
+ transform.position = currentPosition;
221
+ }
222
+
223
+ //オブジェクトをドロップする
224
+ void OnMouseUp()
225
+ {
226
+ Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
227
+ Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset;
228
+
229
+ //currentPositionが範囲内であるか,Gridに値があるかどうかを判定
230
+ if (isValidGridPos())
231
+ {
232
+ transform.position = Grid.roundVec2(currentPosition);
233
+ //攻撃情報
234
+ //ピースの数(子オブジェクトの数)を判定
235
+ int ObjCount = this.transform.childCount;
236
+
237
+
238
+ Character test = GameObject.FindGameObjectWithTag("Party").GetComponent<Party>().GetMember(Party.PARTY_MEMBER.LEADER);
239
+ BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>();
240
+ bm.SetAttackInfo(ObjCount, test, 0, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0);
241
+ bm.BlockSet();
242
+
243
+ count.countUp();
244
+ Grid.deleteFullRows();
245
+ Grid.deleteFullLines();
246
+
247
+ //指定範囲内ではブロックの当たり判定を無くす
248
+ BoxCollider2D collider = GetComponent<BoxCollider2D>();
249
+ collider.enabled = false;
250
+
251
+ //次のブロックを呼び出す
252
+ FindObjectOfType<Spawner>().spawnNext();
253
+
254
+
255
+ }
256
+ else
257
+ {
258
+ //Spawnerオブジェクトの座標を取る
259
+ transform.position = GameObject.Find("Spawner").transform.position;
260
+
261
+ }
262
+
263
+ }
264
+
265
+
266
+ // Use this for initialization
267
+ void Start()
268
+ {
269
+ count = FindObjectOfType<Count>();
270
+ }
271
+
272
+ // Update is called once per frame
273
+ void Update()
274
+ {
275
+ if (isValidGridPos())
276
+ {
277
+ // It's valid. Update grid.
278
+ updateGrid();
279
+ }
280
+
281
+ }
282
+ }
283
+
284
+ ```
285
+
286
+ Spawner.cs
287
+ ```C#
288
+ using System.Collections;
289
+ using System.Collections.Generic;
290
+ using UnityEngine;
291
+
292
+ public class Spawner : MonoBehaviour
293
+ {
294
+
295
+ public GameObject[] groups;
296
+ public void spawnNext()
297
+ {
298
+ //配列の中をランダムで取り出す
299
+ int i = Random.Range(0, groups.Length);
300
+
301
+ //オブジェクトのインスタンスを作成
302
+ Instantiate(groups[i],
303
+ transform.position,
304
+ Quaternion.identity);
305
+ }
306
+
307
+
308
+ void Start()
309
+ {
310
+ spawnNext();
311
+ }
312
+
313
+ }
314
+
315
+
316
+ ```
156
317
  ###試したこと
157
318
  NullReferenceExceptionが設定されていない変数を示しているときに出るエラーということは理解できたのですが、具体的にどのように解決したら良いのかがわからず詰まっています。
158
319
  回答よろしくお願いいたします。

1

書式改善

2017/09/16 18:07

投稿

sumikko6210
sumikko6210

スコア138

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  ###前提・実現したいこと
2
2
 
3
- Unity2Dのパズルをリセットする機能実装中に以下のエラーメッセージが発生しました。
3
+ Unity2Dのパズルをリセットするためにリセットボタン設置して、そこReset.csを紐づけて実行したところ、以下のエラーメッセージが発生しました。
4
4
 
5
5
  ###発生している問題・エラーメッセージ
6
6