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

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

新規登録して質問してみよう
ただいま回答率
85.46%
Unity

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

Q&A

解決済

1回答

510閲覧

NullReferenceExceptionが治らない

退会済みユーザー

退会済みユーザー

総合スコア0

Unity

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

0グッド

0クリップ

投稿2020/02/08 16:56

現状

生成前に参照しているのかゲーム開始直後のみNullReferenceExceptionが出てしまう
ゲームは普通にできる

試したこと

タグをつけてタグがあるときのみ操作できるようにしたがNullReferenceExceptionがでた

エラーコード

NullReferenceException: Object reference not set to an instance of an object
AlphabetGenerator.Update () (at Assets/Scripts/AlphabetGenerator.cs:60)
NullReferenceException: Object reference not set to an instance of an object
AlphabetGenerator.Update () (at Assets/Scripts/AlphabetGenerator.cs:69)*4

該当コード

alphabetgenerator

1public class AlphabetGenerator : MonoBehaviour 2{ 3 public GameObject[] alphabets; 4 public Camera mainCamera; 5 public static int alphabetNum = 0; 6 public bool isGameOver; 7 private GameObject genealpha; 8 public bool isGene; 9 public bool isFall; 10 private int count; 11 Rigidbody2D rigid2D; 12 GameObject gameController; 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 Reset(); 18 rigid2D = GetComponent<Rigidbody2D>(); 19 //開始時にgamecontrollerを見つける 20 gameController = GameObject.FindWithTag("GameController"); 21 } 22 23 public void Reset() 24 { 25 alphabetNum = 0; 26 isGameOver = false; 27 Move.isMoves.Clear(); 28 StartCoroutine(StateReset()); 29 } 30 31 // Update is called once per frame 32 void Update() 33 { 34 if (isGameOver) 35 { 36 return; 37 } 38 if (MoveCheck(Move.isMoves)) 39 { 40 return; 41 } 42 if (!isGene) 43 { 44 if (rigid2D.velocity.magnitude == 0) 45 { 46 Invoke("GenerateAlphabet", 0.5f); 47 isGene = true; 48 } 49 return; 50 } 51 Vector2 v = new Vector2(mainCamera.ScreenToWorldPoint(Input.mousePosition).x, transform.position.y); 52 if (Input.GetMouseButtonUp(0)) 53 { 54 if (!RotateButton.onButtonDown) 55 { 56///////////////////////////////////////////////////////////////////////////////////// 57 60行目 genealpha.GetComponent<Rigidbody2D>().isKinematic = false; 58//////////////////////////////////////////////////////////////////////////////////// 59 alphabetNum++; 60 isFall = true; 61 } 62 RotateButton.onButtonDown = false; 63 } 64 if (Input.GetMouseButton(0)) 65 { 66/////////////////////////////////////////////////////////////////////////////////////// 67 69行目 genealpha.transform.position = v; 68///////////////////////////////////////////////////////////////////////////////////// 69 } 70 71 72 } 73 74 IEnumerator StateReset() 75 { 76 while (!isGameOver) 77 { 78 yield return new WaitUntil(() => isFall); 79 yield return new WaitForSeconds(0.1f); 80 isFall = false; 81 isGene = false; 82 } 83 } 84 85 public void GenerateAlphabet() 86 { 87 if (count == 0) 88 { 89 genealpha = Instantiate(alphabets[Random.Range(0, alphabets.Length)], transform.position, Quaternion.identity); 90 genealpha.GetComponent<Rigidbody2D>().isKinematic = true; 91 gameController.SendMessage("IncreaseScore"); 92 } 93 } 94 95 public void RotateAlphabet() 96 { 97 if (!isFall) 98 { 99 genealpha.transform.Rotate(0, 0, -30); 100 } 101 } 102 103 bool MoveCheck(List<Move.Moving> isMoves) 104 { 105 if (isMoves == null) 106 { 107 return false; 108 } 109 foreach (Move.Moving b in isMoves) 110 { 111 if (b.isMove) 112 { 113 isGene = true; 114 isFall = true; 115 return true; 116 } 117 } 118 return false; 119 } 120 121 public void OnTriggerStay2D(Collider2D collision) 122 { 123 if (collision.CompareTag("Alphabet") || collision.CompareTag("SetAlphabet")) 124 { 125 count = 1; 126 } 127 } 128 129 private void OnTriggerExit2D(Collider2D collision) 130 { 131 count = 0; 132 } 133 134 135 public bool IsGameOver() 136 { 137 return isGameOver; 138 } 139 public void ReDead() 140 { 141 //ぶつかったらフラグを立てる 142 isGameOver = true; 143 144 } 145} 146

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

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

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

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

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

guest

回答1

0

ベストアンサー

「生成前に参照しているのか」との見立てはおそらく妥当だろうと思います。ちょっと見た感じ、ゲーム開始直後でGenerateAlphabetが一度も実行されていない(Invokeによる実行を待機している)タイミングだとそうなりそうな気がしますね。
Updategenealphanullなら後半部分を実行しないよう条件分岐を加えてみてはいかがでしょうか?

C#

1 // Update is called once per frame 2 void Update() 3 { 4 if (isGameOver) 5 { 6 return; 7 } 8 if (MoveCheck(Move.isMoves)) 9 { 10 return; 11 } 12 if (!isGene) 13 { 14 if (rigid2D.velocity.magnitude == 0) 15 { 16 Invoke("GenerateAlphabet", 0.5f); 17 isGene = true; 18 } 19 return; 20 } 21 if (genealpha == null) 22 { 23 return; 24 } 25 Vector2 v = new Vector2(mainCamera.ScreenToWorldPoint(Input.mousePosition).x, transform.position.y); 26 if (Input.GetMouseButtonUp(0)) 27 { 28 if (!RotateButton.onButtonDown) 29 { 30 genealpha.GetComponent<Rigidbody2D>().isKinematic = false; 31 alphabetNum++; 32 isFall = true; 33 } 34 RotateButton.onButtonDown = false; 35 } 36 if (Input.GetMouseButton(0)) 37 { 38 genealpha.transform.position = v; 39 } 40 }

投稿2020/02/08 18:12

Bongo

総合スコア10807

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

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

退会済みユーザー

退会済みユーザー

2020/02/08 18:56

明日の朝してみます!
退会済みユーザー

退会済みユーザー

2020/02/09 06:12

回答ありがとうございます 御指摘通りにするとエラーが無くなりました if (genealpha == null) { return; } else { Vector2 v = new Vector2(mainCamera.ScreenToWorldPoint(Input.mousePosition).x, transform.position.y); if (Input.GetMouseButtonUp(0)) { if (!RotateButton.onButtonDown) { genealpha.GetComponent<Rigidbody2D>().isKinematic = false; alphabetNum++; isFall = true; } RotateButton.onButtonDown = false; } if (Input.GetMouseButton(0)) { genealpha.transform.position = v; } }
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問