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

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

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

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

Unity

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

Q&A

解決済

1回答

5340閲覧

Unity でテスト中に出たエラー"Object reference not set to an instance of an object"の解決方法がわからない

RAIYK

総合スコア2

C#

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

Unity

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

0グッド

0クリップ

投稿2021/01/14 14:08

編集2021/01/15 09:39

前提・実現したいこと

unityでテトリスを作っています。
getcomponentをつかって他のスプリクトを呼び出そうとしたら
NullReferenceException: Object reference not set to an instance of an object
controller.addgrid () (at Assets/controller.cs:122)
controller.move () (at Assets/controller.cs:64)
controller.Update () (at Assets/controller.cs:31)
というエラーがゲームオーバーのテスト中に発生し、色々記事を調べてみましたが中々うまくいきません。

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

NullReferenceException: Object reference not set to an instance of an object controller.addgrid () (at Assets/controller.cs:122) controller.move () (at Assets/controller.cs:64) controller.Update () (at Assets/controller.cs:31)

ソースコード

C#

1//スプリクト名 controller 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class controller : MonoBehaviour 7{ 8 public float falltimer = 1f; 9 public float timecounter; 10 public float waittimer; 11 public GameObject go; 12 13 14 15 public static int WIDTH = 10; 16 public static int HIGHT = 20; 17 18 private static Transform[,] grid = new Transform[WIDTH, HIGHT]; 19 20 public Vector3 rotationPoint; 21 // Start is called before the first frame update 22 void Start() 23 { 24 go = GameObject.Find("spawnb"); 25 } 26 27 // Update is called once per frame 28 void Update() 29 { 30 move(); 31 32 } 33 void move() 34 { 35 //ブロックの移動 36 if (Input.GetKey(KeyCode.A) && Time.time - waittimer >= 0.05) 37 { 38 transform.position += new Vector3(-1, 0, 0); 39 if (!movecontroll()) 40 { 41 transform.position -= new Vector3(-1, 0, 0); 42 } 43 waittimer = Time.time; 44 45 } 46 else if (Input.GetKey(KeyCode.D) && Time.time - waittimer >= 0.05) 47 { 48 transform.position += new Vector3(1, 0, 0); 49 if (!movecontroll()) 50 { 51 transform.position -= new Vector3(1, 0, 0); 52 } 53 waittimer = Time.time; 54 55 } 56 else if (Input.GetKey(KeyCode.S) || Time.time - timecounter >= falltimer) 57 { 58 transform.position += new Vector3(0, -1, 0); 59 60 if (!movecontroll()) 61 { 62 transform.position -= new Vector3(0, -1, 0); 63 addgrid(); 64 checkline(); 65 this.enabled = false; 66 FindObjectOfType<spawn>().spawnblock(); 67 68 } 69 timecounter = Time.time; 70 71 } 72 if (Input.GetKey(KeyCode.W) && Time.time - waittimer >= 0.1) 73 { 74 transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90); 75 if (!movecontroll()) 76 { 77 transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90); 78 } 79 waittimer = Time.time; 80 } 81 } 82 //ブロックの制御 83 bool movecontroll() 84 { 85 foreach (Transform children in transform) 86 { 87 int roundX = Mathf.RoundToInt(children.transform.position.x); 88 int roundY = Mathf.RoundToInt(children.transform.position.y); 89 90 if (roundX < 0 || roundX >= WIDTH || roundY < 0 || roundY >= HIGHT) 91 { 92 return false; 93 } 94 if (grid[roundX, roundY] != null) 95 { 96 return false; 97 } 98 99 } 100 return true; 101 } 102 103 //ブロックが重ならないようにする 104 void addgrid() 105 { 106 foreach (Transform children in transform) 107 { 108 109 int roundX = Mathf.RoundToInt(children.transform.position.x); 110 int roundY = Mathf.RoundToInt(children.transform.position.y); 111 112 grid[roundX, roundY] = children; 113 114 if(roundY>=HIGHT-1) 115 { 116 Destroy(go); 117 Debug.Log("gameover"); 118 GetComponent<gamecontroll>().gameover(); 119 this.enabled = false; 120 } 121 122 } 123 } 124 125 // ラインがあるか?確認 126 public void checkline() 127 { 128 for (int i = HIGHT - 1; i >= 0; i--) 129 { 130 if (HasLine(i)) 131 { 132 DeleteLine(i); 133 RowDown(i); 134 } 135 } 136 } 137 138 // 列がそろっているか?確認 139 bool HasLine(int i) 140 { 141 for (int j = 0; j < WIDTH; j++) 142 { 143 if (grid[j, i] == null) 144 return false; 145 } 146 return true; 147 } 148 149 // ラインを消す 150 void DeleteLine(int i) 151 { 152 for (int j = 0; j < WIDTH; j++) 153 { 154 Destroy(grid[j, i].gameObject); 155 grid[j, i] = null; 156 } 157 158 } 159 160 // 列を下げる 161 public void RowDown(int i) 162 { 163 for (int y = i; y < HIGHT; y++) 164 { 165 for (int j = 0; j < WIDTH; j++) 166 { 167 if (grid[j, y] != null) 168 { 169 grid[j, y - 1] = grid[j, y]; 170 grid[j, y] = null; 171 grid[j, y - 1].transform.position -= new Vector3(0, 1, 0); 172 } 173 } 174 } 175 } 176} 177

c#

1//スプリクト名 spawn 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6public class spawn : MonoBehaviour 7{ 8 public GameObject[] tetris; 9 // Start is called before the first frame update 10 void Start() 11 { 12 spawnblock(); 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 19 } 20 21 public void spawnblock() 22 { 23 Instantiate(tetris[Random.Range(0, tetris.Length)], transform.position, Quaternion.identity); 24 } 25 26 27}

c#

1//スプリクト名 gamecontroll 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5 6 7public class gamecontroll : MonoBehaviour 8{ 9 // Start is called before the first frame update 10 void Start() 11 { 12 this.gameObject.SetActive(false); 13 } 14 15 // Update is called once per frame 16 void Update() 17 { 18 19 } 20 public void gameover() 21 { 22 this.gameObject.SetActive(true); 23 } 24}

補足情報

ゲームエンジンver 2019.4.14f1

以下のサイトを参考にして制作しました。
https://masavlog.com/tetris/unity-tetris-2/
https://www.youtube.com/watch?v=T5P8ohdxDjo

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

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

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

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

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

guest

回答1

0

ベストアンサー

GetComponent<>()を利用してエラーが発生したとのことですが,恐らくaddgridメソッド内の
GetComponent<gamecontroll>().gameover();
の部分だとは思いますがこのスクリプトをアタッチしているゲームオブジェクトにgamecontrollコンポーネントはアタッチされていますでしょうか?
されていない場合はそちらが原因かもしれません.

投稿2021/01/18 14:39

Yothuba3

総合スコア17

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問