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

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

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

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

Q&A

解決済

1回答

1072閲覧

Unityの教科書 2019のch5の矢が当たったときにUIを変更する

n_nakai

総合スコア1

C#

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

0グッド

0クリップ

投稿2020/06/24 09:36

編集2020/06/24 10:00

前提・実現したいこと

矢に当たるとHPゲージが減る

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

NullReferenceException: Object reference not set to an instance of an object
GameDirector.DecreaseHp () (at Assets/GameDirector.cs:18)
ArrowController.Update () (at Assets/ArrowController.cs:31)

該当のソースコード

using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class ArrowGenerator : MonoBehaviour { public GameObject arrowPrefab; float span = 1.0f; float delta = 0; // Start is called before the first frame update // Update is called once per frame void Update() { this.delta += Time.deltaTime; if(this.delta > this.span) { this.delta = 0; GameObject go = Instantiate(arrowPrefab) as GameObject; int px = UnityEngine.Random.Range(-6, 7); go.transform.position = new Vector3(px, 7, 0); } } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class ArrowController : MonoBehaviour { GameObject player; // Start is called before the first frame update void Start() { this.player = GameObject.Find("player"); } // Update is called once per frame void Update() { transform.Translate(0, -0.1f, 0); if(transform.position.y < -5.0f) { Destroy(gameObject); } Vector2 p1 = transform.position; Vector2 p2 = this.player.transform.position; Vector2 dir = p1 - p2; float d = dir.magnitude; float r1 = 0.5f; float r2 = 1.0f; if(d < r1 + r2) { GameObject director = GameObject.Find("GameDirector"); director.GetComponent<GameDirector>().DecreaseHp(); Destroy(gameObject); } } } using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameDirector : MonoBehaviour { GameObject hpGauge; // Start is called before the first frame update void Start() { this.hpGauge = GameObject.Find("hpGause"); } // Update is called once per frame public void DecreaseHp() { this.hpGauge.GetComponent<Image>().fillAmount -= 0.1f; } }

試したこと

エラーメッセージの部分のスペルミスがないか確認した

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

Unity 2019.4.0f1

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

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

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

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

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

y_waiwai

2020/06/24 09:43

このままではコードが見づらいです。質問を編集し、<code>ボタンを押し、出てきた’’’の枠の中にコードを貼り付けてください また、エラーメッセージはそのままコピペで提示しましょう
n_nakai

2020/06/24 09:50

エラーメッセージはこれと同じものが、実行すると30個以上表示されます。
fiveHundred

2020/06/24 09:58

エラー発生箇所である「GameDirector」のコードが無いので、追記してください。
guest

回答1

0

ベストアンサー

以下のどちらかがnullになっているのが原因だと思います。
Debug.Log()やデバッガでどちらがnullになっているのか確認して、対応してください。

  • this.hpGaugeがnull

この場合、Start()のGameObject.Find("hpGause");が失敗してnullになっている可能性が高いです。
「hpGause」というゲームオブジェクトが存在するかやスペルが間違っていないかを確認してください。
(個人的にはGameObject.Find()で参照するのはあまり好きではないので、hpGaugeにpublic[SerializeField]を付けてインスペクターから設定するほうが多いです)

  • this.hpGauge.GetComponent<Image>()がnull

この場合、「this.hpGauge」にImageがアタッチされていません。
「this.hpGauge」にImageがアタッチされているかどうか確認してください。
もし、「this.hpGauge」ではなく、その子オブジェクトにアタッチされている場合は、代わりにGetComponentInChildren<Image>()を使ってください。

投稿2020/06/24 10:22

fiveHundred

総合スコア9796

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

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

n_nakai

2020/06/24 10:42

解決しました。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問