質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

2536閲覧

Unity 2Dでのエラー"NullReferenceException"を解決したい

sumikko6210

総合スコア138

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2017/09/16 13:37

編集2017/09/16 18:08

###前提・実現したいこと
イメージ説明
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

ソースが上記のものだけならば、
public static Transform[,] grid = new Transform[w, h];
は配列サイズ分だけ入る場所を用意しただけであって、中身はnullです。
(grid[0,0]からgrid[7,7]まで全部null)

Transformを作るなりして、入れてあげないといけません。

投稿2017/09/16 16:31

rururu3

総合スコア5545

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sumikko6210

2017/09/16 18:12

ご回答ありがとうございます。 使用しているコードすべてを追加しました。 ご確認お願いします。 パズルをはめた後も同じNullReferenceExceptionエラーが起こってしまう状況です。。。
rururu3

2017/09/16 18:41

deleteReset deleteRow deleteLine も isRowFull と同じようにnullチェックしてからDestroy(grid[x, y].gameObject);やるべきでは
sumikko6210

2017/09/19 17:01

nullチェックをしたらうまく修正できました! 回答ありがとうございました。 ベストアンサーにさせていただきます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問