###前提・実現したいこと
Unity2Dのパズルをリセットするためにリセットボタンを設置して、そこにReset.csを紐づけて実行したところ、以下のエラーメッセージが発生しました。
###発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object Grid.deleteReset () (at Assets/Script/Grid.cs:31) Reset.OnMouseDown () (at Assets/Script/Reset.cs:19) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
###該当のソースコード
Grid.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Grid: MonoBehaviour { 6 public static int w = 8; 7 public static int h = 8; 8 public static Transform[,] grid = new Transform[w, h]; 9 10 //座標を整数にする 11 public static Vector2 roundVec2(Vector2 v) 12 { 13 return new Vector2(Mathf.Round(v.x), 14 Mathf.Round(v.y)); 15 } 16 17 //指定範囲内かどうかを判定 18 public static bool insideBorder(Vector2 pos) 19 { 20 return ((int)pos.x >= 0 && 21 (int)pos.x < w && 22 (int)pos.y >= 0 && 23 (int)pos.y < h); 24 } 25 26 //盤面をリセット 27 public static void deleteReset() 28 { 29 for (int y = 0; y < h; ++y) 30 for (int x = 0; x < w; ++x) 31 { 32 Destroy(grid[x, y].gameObject); 33 grid[x, y] = null; 34 } 35 } 36 37 //横列を消去 38 public static void deleteRow(int y) 39 { 40 for(int x = 0; x < w; ++x) 41 { 42 Destroy(grid[x, y].gameObject); 43 grid[x, y] = null; 44 } 45 } 46 47 //縦列を消去 48 public static void deleteLine(int x) 49 { 50 for (int y = 0; y < h; ++y) 51 { 52 Destroy(grid[x, y].gameObject); 53 grid[x, y] = null; 54 } 55 } 56 57 //横列が埋まっているかどうかを判定 58 public static bool isRowFull(int y) 59 { 60 for (int x = 0; x < w; ++x) 61 if (grid[x, y] == null) 62 return false; 63 return true; 64 } 65 66 //縦列が埋まっているかどうかを判定 67 public static bool isLineFull(int x) 68 { 69 for (int y = 0; y < h; ++y) 70 if (grid[x, y] == null) 71 return false; 72 return true; 73 } 74 75 76 public static void deleteFullRows() 77 { 78 for(int y = 0; y < h; ++y){ 79 if (isRowFull(y)) 80 { 81 deleteRow(y); 82 Character test = GameObject.FindGameObjectWithTag("Party").GetComponent<Party>().GetMember(Party.PARTY_MEMBER.LEADER); 83 BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>(); 84 bm.SetAttackInfo(0, test, 1, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0); 85 bm.DeleteLine(); 86 } 87 } 88 } 89 90 public static void deleteFullLines() 91 { 92 for (int x = 0; x < w; ++x){ 93 if (isLineFull(x)) 94 { 95 deleteLine(x); 96 } 97 } 98 } 99} 100
Reset.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Reset : MonoBehaviour { 6 private Vector3 screenPoint; 7 private Vector3 offset; 8 9 void OnMouseDown() 10 { 11 // マウスカーソルは、スクリーン座標なので、 12 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 13 14 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 15 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 16 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 17 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 18 19 Grid.deleteReset(); 20 21 } 22 23 // Use this for initialization 24 void Start () { 25 26 } 27 28 // Update is called once per frame 29 void Update () { 30 31 } 32} 33
Group.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Group : MonoBehaviour 6{ 7 private Vector3 screenPoint; 8 private Vector3 offset; 9 private Count count; 10 11 12 //座標が範囲内であるか,Gridに値があるかどうかを判定 13 public bool isValidGridPos() 14 { 15 foreach (Transform child in transform) 16 { 17 Vector2 v = Grid.roundVec2(child.position); 18 19 if (!Grid.insideBorder(v)) 20 return false; 21 22 if (Grid.grid[(int)v.x, (int)v.y] != null && 23 Grid.grid[(int)v.x, (int)v.y].parent != transform) 24 return false; 25 } 26 return true; 27 } 28 29 void updateGrid() 30 { 31 for (int y = 0; y < Grid.h; ++y) 32 for (int x = 0; x < Grid.w; ++x) 33 if (Grid.grid[x, y] != null) 34 if (Grid.grid[x, y].parent == transform) 35 Grid.grid[x, y] = null; 36 37 foreach (Transform child in transform) 38 { 39 Vector2 v = Grid.roundVec2(child.position); 40 Grid.grid[(int)v.x, (int)v.y] = child; 41 } 42 } 43 44 //オブジェクトをクリックする 45 void OnMouseDown() 46 { 47 // マウスカーソルは、スクリーン座標なので、 48 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 49 50 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 51 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 52 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 53 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 54 55 56 } 57 58 //オブジェクトをドラッグする 59 void OnMouseDrag() 60 { 61 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 62 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 63 transform.position = currentPosition; 64 } 65 66 //オブジェクトをドロップする 67 void OnMouseUp() 68 { 69 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 70 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 71 72 //currentPositionが範囲内であるか,Gridに値があるかどうかを判定 73 if (isValidGridPos()) 74 { 75 transform.position = Grid.roundVec2(currentPosition); 76 //攻撃情報 77 //ピースの数(子オブジェクトの数)を判定 78 int ObjCount = this.transform.childCount; 79 80 81 Character test = GameObject.FindGameObjectWithTag("Party").GetComponent<Party>().GetMember(Party.PARTY_MEMBER.LEADER); 82 BattleManager bm = GameObject.FindGameObjectWithTag("BattleManager").GetComponent<BattleManager>(); 83 bm.SetAttackInfo(ObjCount, test, 0, 0, 0, BattleManager.TYPE.SPRING, 0, 0, 0, 0, 0); 84 bm.BlockSet(); 85 86 count.countUp(); 87 Grid.deleteFullRows(); 88 Grid.deleteFullLines(); 89 90 //指定範囲内ではブロックの当たり判定を無くす 91 BoxCollider2D collider = GetComponent<BoxCollider2D>(); 92 collider.enabled = false; 93 94 //次のブロックを呼び出す 95 FindObjectOfType<Spawner>().spawnNext(); 96 97 98 } 99 else 100 { 101 //Spawnerオブジェクトの座標を取る 102 transform.position = GameObject.Find("Spawner").transform.position; 103 104 } 105 106 } 107 108 109 // Use this for initialization 110 void Start() 111 { 112 count = FindObjectOfType<Count>(); 113 } 114 115 // Update is called once per frame 116 void Update() 117 { 118 if (isValidGridPos()) 119 { 120 // It's valid. Update grid. 121 updateGrid(); 122 } 123 124 } 125} 126
Spawner.cs
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Spawner : MonoBehaviour 6{ 7 8 public GameObject[] groups; 9 public void spawnNext() 10 { 11 //配列の中をランダムで取り出す 12 int i = Random.Range(0, groups.Length); 13 14 //オブジェクトのインスタンスを作成 15 Instantiate(groups[i], 16 transform.position, 17 Quaternion.identity); 18 } 19 20 21 void Start() 22 { 23 spawnNext(); 24 } 25 26} 27 28
###試したこと
NullReferenceExceptionが設定されていない変数を示しているときに出るエラーということは理解できたのですが、具体的にどのように解決したら良いのかがわからず詰まっています。
回答よろしくお願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
Unity 5.6.2f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/16 18:12
2017/09/16 18:41
2017/09/19 17:01