前提・実現したいこと
ttps://tech.pjin.jp/blog/2015/09/06/unity%e3%81%a72d%e3%82%a2%e3%82%af%e3%82%b7%e3%83%a7%e3%83%b3%e3%82%b2%e3%83%bc%e3%83%a0%e3%82%92%e4%bd%9c%e3%82%8d%e3%81%86%e2%91%a3-%ef%bd%9e/
を写しながら、Unityでゲームを作成しています。その際にエラーが発生しました。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Script/Player.cs:40)
該当のソースコード(C#)
Player
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player : MonoBehaviour 6{ 7 public float flap = 600f; 8 public float scroll = 10f; 9 Rigidbody2D rigidbody2D; 10 GameObject gameController; 11 GameObject scoreGUI; 12 13 void Awake() 14 { 15 rigidbody2D = GetComponent<Rigidbody2D>(); 16 gameController = GameObject.Find("GameController"); 17 scoreGUI = GameObject.Find("ScoreGUI"); 18 } 19 20 void Start() 21 { 22 rigidbody2D.isKinematic = true; 23 } 24 25 void FixedUpdate() 26 { 27 if (GameController.isPlaying == true) 28 { 29 rigidbody2D.velocity = new Vector2(scroll, rigidbody2D.velocity.y); 30 } 31 } 32 33 void Update() 34 { 35 if (Input.GetKeyDown("space")) 36 { 37 38 if (GameController.isPlaying == false) 39 { 40 gameController.SendMessage("GameStart"); 41 rigidbody2D.isKinematic = false; 42 } 43 rigidbody2D.velocity = Vector2.zero; 44 rigidbody2D.AddForce(Vector2.up * flap, ForceMode2D.Impulse); 45 } 46 } 47 48 void Move() 49 { 50 if (GameController.isPlaying == true) 51 { 52 rigidbody2D.velocity = new Vector2(scroll, rigidbody2D.velocity.y); 53 } 54 } 55 56 void OnTriggerEnter2D(Collider2D col) 57 { 58 if (col.gameObject.tag == "CountZone") 59 { 60 scoreGUI.SendMessage("AddScore", 1); 61 } 62 } 63 64 void OnCollisionEnter2D(Collision2D col) 65 { 66 if (col.gameObject.tag == "Death") 67 { 68 gameController.SendMessage("GameOver"); 69 } 70 } 71}
GameController
1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.UI; 6using UnityEngine.SceneManagement; 7 8public class GameController : MonoBehaviour 9{ 10 11 private GameObject player; 12 private GameObject start; 13 private GameObject gameOver; 14 public static bool isPlaying; 15 16 void Awake() 17 { 18 player = GameObject.Find("Player"); 19 start = GameObject.Find("Start"); 20 gameOver = GameObject.Find("GameOver"); 21 } 22 23 void Start() 24 { 25 gameOver.SetActive(false); 26 isPlaying = false; 27 } 28 29 void Update() 30 { 31 if (Input.GetKeyDown("a")) 32 { 33 SceneManager.LoadScene("bird"); 34 } 35 } 36 37 public void GameStart() 38 { 39 start.SetActive(false); 40 isPlaying = true; 41 } 42 43 public void GameOver() 44 { 45 if (isPlaying == true) 46 { 47 gameOver.SetActive(true); 48 isPlaying = false; 49 player.GetComponent<Player>().enabled = false; 50 } 51 } 52}
試したこと
エラーの日本語訳を調べる
入力するキーを変えてみる
補足情報
Playerで、エラーではないのですが
Rigidbody2D rigidbody2D;
の部分で
「'Player.rigidbody2D'は継承されたメンバー'Conponent.rigidbody2D'を非表示にします。
非表示にする場合はキーワードnewを使用してください。」
と出ています。
また、GameControllerは'Start'と表示するテキストに入れています。
現在実行すると初期位置から落ちず、動きません。エラーはspaceキーを押したときに表示されます。
回答2件
あなたの回答
tips
プレビュー