質問編集履歴
3
画像の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
###前提・実現したいこと
|
2
2
|
|
3
|
-
|
3
|
+
![イメージ説明](cf744b8418294c22be42b07dbc389c26.png)
|
4
4
|
|
5
5
|
Unity2Dのパズルをリセットするためにリセットボタンを設置して、そこにReset.csを紐づけて実行したところ、以下のエラーメッセージが発生しました。
|
6
6
|
|
2
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -308,6 +308,328 @@
|
|
308
308
|
|
309
309
|
|
310
310
|
|
311
|
+
Group.cs
|
312
|
+
|
313
|
+
```C#
|
314
|
+
|
315
|
+
using System.Collections;
|
316
|
+
|
317
|
+
using System.Collections.Generic;
|
318
|
+
|
319
|
+
using UnityEngine;
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
public class Group : MonoBehaviour
|
324
|
+
|
325
|
+
{
|
326
|
+
|
327
|
+
private Vector3 screenPoint;
|
328
|
+
|
329
|
+
private Vector3 offset;
|
330
|
+
|
331
|
+
private Count count;
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
//座標が範囲内であるか,Gridに値があるかどうかを判定
|
338
|
+
|
339
|
+
public bool isValidGridPos()
|
340
|
+
|
341
|
+
{
|
342
|
+
|
343
|
+
foreach (Transform child in transform)
|
344
|
+
|
345
|
+
{
|
346
|
+
|
347
|
+
Vector2 v = Grid.roundVec2(child.position);
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
if (!Grid.insideBorder(v))
|
352
|
+
|
353
|
+
return false;
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
if (Grid.grid[(int)v.x, (int)v.y] != null &&
|
358
|
+
|
359
|
+
Grid.grid[(int)v.x, (int)v.y].parent != transform)
|
360
|
+
|
361
|
+
return false;
|
362
|
+
|
363
|
+
}
|
364
|
+
|
365
|
+
return true;
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
void updateGrid()
|
372
|
+
|
373
|
+
{
|
374
|
+
|
375
|
+
for (int y = 0; y < Grid.h; ++y)
|
376
|
+
|
377
|
+
for (int x = 0; x < Grid.w; ++x)
|
378
|
+
|
379
|
+
if (Grid.grid[x, y] != null)
|
380
|
+
|
381
|
+
if (Grid.grid[x, y].parent == transform)
|
382
|
+
|
383
|
+
Grid.grid[x, y] = null;
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
foreach (Transform child in transform)
|
388
|
+
|
389
|
+
{
|
390
|
+
|
391
|
+
Vector2 v = Grid.roundVec2(child.position);
|
392
|
+
|
393
|
+
Grid.grid[(int)v.x, (int)v.y] = child;
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
//オブジェクトをクリックする
|
402
|
+
|
403
|
+
void OnMouseDown()
|
404
|
+
|
405
|
+
{
|
406
|
+
|
407
|
+
// マウスカーソルは、スクリーン座標なので、
|
408
|
+
|
409
|
+
// 対象のオブジェクトもスクリーン座標に変換してから計算する。
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
// このオブジェクトの位置(transform.position)をスクリーン座標に変換。
|
414
|
+
|
415
|
+
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
|
416
|
+
|
417
|
+
// ワールド座標上の、マウスカーソルと、対象の位置の差分。
|
418
|
+
|
419
|
+
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
//オブジェクトをドラッグする
|
430
|
+
|
431
|
+
void OnMouseDrag()
|
432
|
+
|
433
|
+
{
|
434
|
+
|
435
|
+
Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
|
436
|
+
|
437
|
+
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset;
|
438
|
+
|
439
|
+
transform.position = currentPosition;
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
//オブジェクトをドロップする
|
446
|
+
|
447
|
+
void OnMouseUp()
|
448
|
+
|
449
|
+
{
|
450
|
+
|
451
|
+
Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
|
452
|
+
|
453
|
+
Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset;
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
//currentPositionが範囲内であるか,Gridに値があるかどうかを判定
|
458
|
+
|
459
|
+
if (isValidGridPos())
|
460
|
+
|
461
|
+
{
|
462
|
+
|
463
|
+
transform.position = Grid.roundVec2(currentPosition);
|
464
|
+
|
465
|
+
//攻撃情報
|
466
|
+
|
467
|
+
//ピースの数(子オブジェクトの数)を判定
|
468
|
+
|
469
|
+
int ObjCount = this.transform.childCount;
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
Character test = GameObject.FindGameObjectWithTag("Party").GetComponent<Party>().GetMember(Party.PARTY_MEMBER.LEADER);
|
476
|
+
|
477
|
+
BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>();
|
478
|
+
|
479
|
+
bm.SetAttackInfo(ObjCount, test, 0, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0);
|
480
|
+
|
481
|
+
bm.BlockSet();
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
count.countUp();
|
486
|
+
|
487
|
+
Grid.deleteFullRows();
|
488
|
+
|
489
|
+
Grid.deleteFullLines();
|
490
|
+
|
491
|
+
|
492
|
+
|
493
|
+
//指定範囲内ではブロックの当たり判定を無くす
|
494
|
+
|
495
|
+
BoxCollider2D collider = GetComponent<BoxCollider2D>();
|
496
|
+
|
497
|
+
collider.enabled = false;
|
498
|
+
|
499
|
+
|
500
|
+
|
501
|
+
//次のブロックを呼び出す
|
502
|
+
|
503
|
+
FindObjectOfType<Spawner>().spawnNext();
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
else
|
512
|
+
|
513
|
+
{
|
514
|
+
|
515
|
+
//Spawnerオブジェクトの座標を取る
|
516
|
+
|
517
|
+
transform.position = GameObject.Find("Spawner").transform.position;
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
}
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
}
|
526
|
+
|
527
|
+
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
// Use this for initialization
|
532
|
+
|
533
|
+
void Start()
|
534
|
+
|
535
|
+
{
|
536
|
+
|
537
|
+
count = FindObjectOfType<Count>();
|
538
|
+
|
539
|
+
}
|
540
|
+
|
541
|
+
|
542
|
+
|
543
|
+
// Update is called once per frame
|
544
|
+
|
545
|
+
void Update()
|
546
|
+
|
547
|
+
{
|
548
|
+
|
549
|
+
if (isValidGridPos())
|
550
|
+
|
551
|
+
{
|
552
|
+
|
553
|
+
// It's valid. Update grid.
|
554
|
+
|
555
|
+
updateGrid();
|
556
|
+
|
557
|
+
}
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
}
|
562
|
+
|
563
|
+
}
|
564
|
+
|
565
|
+
|
566
|
+
|
567
|
+
```
|
568
|
+
|
569
|
+
|
570
|
+
|
571
|
+
Spawner.cs
|
572
|
+
|
573
|
+
```C#
|
574
|
+
|
575
|
+
using System.Collections;
|
576
|
+
|
577
|
+
using System.Collections.Generic;
|
578
|
+
|
579
|
+
using UnityEngine;
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
public class Spawner : MonoBehaviour
|
584
|
+
|
585
|
+
{
|
586
|
+
|
587
|
+
|
588
|
+
|
589
|
+
public GameObject[] groups;
|
590
|
+
|
591
|
+
public void spawnNext()
|
592
|
+
|
593
|
+
{
|
594
|
+
|
595
|
+
//配列の中をランダムで取り出す
|
596
|
+
|
597
|
+
int i = Random.Range(0, groups.Length);
|
598
|
+
|
599
|
+
|
600
|
+
|
601
|
+
//オブジェクトのインスタンスを作成
|
602
|
+
|
603
|
+
Instantiate(groups[i],
|
604
|
+
|
605
|
+
transform.position,
|
606
|
+
|
607
|
+
Quaternion.identity);
|
608
|
+
|
609
|
+
}
|
610
|
+
|
611
|
+
|
612
|
+
|
613
|
+
|
614
|
+
|
615
|
+
void Start()
|
616
|
+
|
617
|
+
{
|
618
|
+
|
619
|
+
spawnNext();
|
620
|
+
|
621
|
+
}
|
622
|
+
|
623
|
+
|
624
|
+
|
625
|
+
}
|
626
|
+
|
627
|
+
|
628
|
+
|
629
|
+
|
630
|
+
|
631
|
+
```
|
632
|
+
|
311
633
|
###試したこと
|
312
634
|
|
313
635
|
NullReferenceExceptionが設定されていない変数を示しているときに出るエラーということは理解できたのですが、具体的にどのように解決したら良いのかがわからず詰まっています。
|
1
書式改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
-
Unity2Dのパズルをリセットする
|
5
|
+
Unity2Dのパズルをリセットするためにリセットボタンを設置して、そこにReset.csを紐づけて実行したところ、以下のエラーメッセージが発生しました。
|
6
6
|
|
7
7
|
|
8
8
|
|