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

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

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

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

Q&A

解決済

2回答

1619閲覧

NullReferenceExceptionのエラーメッセージに困っています。

Rikkn

総合スコア0

C#

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

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

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

0グッド

1クリップ

投稿2021/06/14 16:56

編集2021/06/14 19:42

前提・実現したいこと

マインスイーパーを作っているのですが空白のセルを押すと以下のエラーメッセージが出ました。

発生している問題・エラーメッセージ

NullReferenceException: Object reference not set to an instance of an object Cell.OnclickThis () (at Assets/Cell.cs:64) UnityEngine.Events.InvokableCall.Invoke () (at <480508088aee40cab70818ff164a29d5>:0) UnityEngine.Events.UnityEvent.Invoke () (at <480508088aee40cab70818ff164a29d5>:0) UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2019.3.13f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

該当のソースコード

C#

1using UnityEngine; 2using UnityEngine.UI; 3 4public class Cell : MonoBehaviour 5{ 6 public Minesweeper minesweeper; 7 public Vector2Int positionCell; 8 public bool Open; 9 10 [SerializeField] 11 public GameObject m_button = null; 12 13 [SerializeField] 14 private Text _view = null; 15 16 [SerializeField] 17 private CellState _cellState = CellState.None; 18 public CellState CellState 19 { 20 get => _cellState; 21 set 22 { 23 _cellState = value; 24 OnCellStateChanged(); 25 } 26 } 27 28 private void OnValidate() 29 { 30 OnCellStateChanged(); 31 } 32 33 private void OnCellStateChanged() 34 { 35 if (_view == null) { return; } 36 37 if (_cellState == CellState.None) 38 { 39 _view.text = ""; 40 } 41 else if (_cellState == CellState.Mine) 42 { 43 _view.text = "X"; 44 _view.color = Color.red; 45 } 46 else 47 { 48 _view.text = ((int)_cellState).ToString(); 49 _view.color = Color.blue; 50 } 51 } 52 53 public void CellOpen() 54 { 55 m_button.SetActive(false); 56 Open = true; 57 } 58 public void OnclickThis() 59 { 60 if (_cellState == CellState.None) 61 { 62 m_button.SetActive(false); 63 Open = true; 64 minesweeper.OpenCell(this); 65 } 66 else if (_cellState != CellState.Mine) 67 { 68 m_button.SetActive(false); 69 Open = true; 70 } 71 else 72 { 73 m_button.SetActive(false); 74 Open = true; 75 } 76 } 77} 78public enum CellState 79{ 80 None = 0, 81 One = 1, 82 Two = 2, 83 Three = 3, 84 Four = 4, 85 Five = 5, 86 Six = 6, 87 Seven = 7, 88 Eight = 8, 89 90 Mine = -1, 91}

C#

1using System.Collections.Generic; 2using UnityEngine; 3using UnityEngine.UI; 4 5public class Minesweeper : MonoBehaviour 6{ 7 [SerializeField] 8 private int _rows = 1; 9 10 [SerializeField] 11 private int _columns = 1; 12 13 [SerializeField] 14 private int _mineCount = 1; 15 16 [SerializeField] 17 private GridLayoutGroup _gridLayoutGroup = null; 18 19 [SerializeField] 20 private Cell _cellPrefab = null; 21 22 [SerializeField] 23 private Cell[,] _cells; 24 25 void Start() 26 { 27 var parent = _gridLayoutGroup.gameObject.transform; 28 if (_columns < _rows) 29 { 30 _gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount; 31 _gridLayoutGroup.constraintCount = _columns; 32 } 33 else 34 { 35 _gridLayoutGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount; 36 _gridLayoutGroup.constraintCount = _rows; 37 } 38 39 _cells = new Cell[_rows, _columns]; 40 for (var r = 0; r < _rows; r++) 41 { 42 for (var c = 0; c < _columns; c++) 43 { 44 var cell = Instantiate(_cellPrefab); 45 cell.transform.SetParent(parent); 46 _cells[r, c] = cell; 47 } 48 } 49 50 for (var i = 0; i < _mineCount;) 51 { 52 var r = Random.Range(0, _rows); 53 var c = Random.Range(0, _columns); 54 var cell = _cells[r, c]; 55 if (cell.CellState != CellState.Mine) 56 { 57 cell.CellState = CellState.Mine; 58 i++; 59 } 60 } 61 for (var r = 0; r < _rows; r++) 62 { 63 for (var c = 0; c < _columns; c++) 64 { 65 if (_cells[r, c].CellState == CellState.Mine) 66 { 67 AddMine(r, c); 68 } 69 } 70 } 71 72 //for (var r = 0; r < _rows; r++) 73 //{ 74 // for (var c = 0; c < _columns; c++) 75 // { 76 // // 周辺の地雷数を設定したいセル 77 // var cell = _cells[r, c]; 78 // Debug.Log($"Cell[{r}, {c}]={cell.CellState}"); 79 80 // if (cell.CellState == CellState.Mine) 81 // { 82 // continue; // 地雷なら無視する 83 // } 84 85 // // 以下、泥臭く周辺のセルが地雷かどうか調べるコード 86 // // ただしそのままだと不正なインデックスになるので注意 87 // var count = 0; 88 // if (_cells[r - 1, c - 1].CellState == CellState.Mine) 89 // { 90 // count++; 91 // } 92 // if (_cells[r - 1, c].CellState == CellState.Mine) 93 // { 94 // count++; 95 // } 96 // // etc... 97 98 // // 最後に count をセルに設定する 99 // } 100 //} 101 } 102 103 public void AddMine(int r, int c) 104 { 105 var left = c - 1; 106 var right = c + 1; 107 var top = r - 1; 108 var bottom = r + 1; 109 110 if (top >= 0) 111 { 112 if (left >= 0 && _cells[top, left].CellState != CellState.Mine) { _cells[top, left].CellState++; } 113 if (_cells[top, c].CellState != CellState.Mine) { _cells[top, c].CellState++; } 114 if (right < _columns && _cells[top, right].CellState != CellState.Mine) { _cells[top, right].CellState++; } 115 } 116 if (left >= 0 && _cells[r, left].CellState != CellState.Mine) { _cells[r, left].CellState++; } 117 if (right < _columns && _cells[r, right].CellState != CellState.Mine) { _cells[r, right].CellState++; } 118 if (bottom < _rows) 119 { 120 if (left >= 0 && _cells[bottom, left].CellState != CellState.Mine) { _cells[bottom, left].CellState++; } 121 if (_cells[bottom, c].CellState != CellState.Mine) { _cells[bottom, c].CellState++; } 122 if (right < _columns && _cells[bottom, right].CellState != CellState.Mine) { _cells[bottom, right].CellState++; } 123 } 124 } 125 public int GetMineCount(int r, int c) 126 { 127 var left = c - 1; 128 var right = c + 1; 129 var top = r - 1; 130 var bottom = r + 1; 131 132 var count = 0; 133 if (top >= 0) 134 { 135 if (left >= 0 && _cells[top, left].CellState == CellState.Mine) { count++; } 136 if (_cells[top, c].CellState == CellState.Mine) { count++; } 137 if (right < _columns && _cells[top, right].CellState == CellState.Mine) { count++; } 138 } 139 if (left >= 0 && _cells[r, left].CellState == CellState.Mine) { count++; } 140 if (right < _columns && _cells[r, right].CellState == CellState.Mine) { count++; } 141 if (bottom < _rows) 142 { 143 if (left >= 0 && _cells[bottom, left].CellState == CellState.Mine) { count++; } 144 if (_cells[bottom, c].CellState == CellState.Mine) { count++; } 145 if (right < _columns && _cells[bottom, right].CellState == CellState.Mine) { count++; } 146 } 147 return count; 148 } 149 private Cell[] SearchCell(int r, int c) 150 { 151 var list = new List<Cell>(); 152 153 var left = c - 1; 154 var right = c + 1; 155 var top = r - 1; 156 var bottom = r + 1; 157 158 if (top >= 0) 159 { 160 if (left >= 0) { list.Add(_cells[top, left]); } 161 list.Add(_cells[top, c]); 162 if (right < _columns) { list.Add(_cells[top, right]); } 163 } 164 if (left >= 0) { list.Add(_cells[r, left]); } 165 if (right < _columns) { list.Add(_cells[r, right]); } 166 if (bottom < _rows) 167 { 168 if (left >= 0) { list.Add(_cells[bottom, left]); } 169 list.Add(_cells[bottom, c]); 170 if (right < _columns) { list.Add(_cells[bottom, right]); } 171 } 172 173 return list.ToArray(); 174 } 175 176 public void OpenCell(Cell target) 177 { 178 //自分の座標を記憶 179 var x = target.positionCell.x; 180 var y = target.positionCell.y; 181 182 foreach (var item in SearchCell(x, y)) 183 { 184 if (item.Open) //既にあいてたら次のセルへ 185 { 186 continue; 187 } 188 item.CellOpen(); //セルをあけてフラグをtrueにする 189 if (item.CellState == CellState.None) 190 { 191 OpenCell(item); //Noneなら再帰でNoneを探してあけていく 192 } 193 } 194 } 195}

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

2019.3.13f1

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

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

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

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

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

guest

回答2

0

そいつはNullの変数を参照してるときに出ます
そこに出てるとこらへんで実行を止め、片っ端から変数のナカミをチェックしてみよう

投稿2021/06/14 21:33

y_waiwai

総合スコア87774

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

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

0

自己解決

c#

1for (var r = 0; r < _rows; r++) 2 { 3 for (var c = 0; c < _columns; c++) 4 { 5 var cell = Instantiate(_cellPrefab); 6 cell.positionCell = new Vector2Int(r, c); 7 //cell.minesweeper = this; 8 cell.transform.SetParent(parent); 9 _cells[r, c] = cell; 10 } 11 }

コメントアウトした一行を追加した。

投稿2021/06/15 05:43

Rikkn

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問