プレハブに付属しているスクリプトを取得したいです
現在、3Dゲームを制作しています。
Canvas内にあるHPのイメージをプレーヤーのHPを参照して
FillAmountで縮小させたいのですが、プレーヤーにアタッチされているスクリプト情報を取得しようとすると再生時に参照が外れてしまいます。
取得する方法、またはなにか間違いなどがあれば
お教えいただきたく存じます。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object
該当のソースコード
Cここに言語名を入力C#
1 2 3・GUIスクリプト 4 5using System.Collections.Generic; 6using UnityEngine; 7using UnityEngine.UI; 8 9public class HpGauge : MonoBehaviour 10{ 11 [SerializeField] 12 private GameObject m_Player; 13 private PlayerMove m_PlayerScript; 14 private Image m_HPimage; 15 16 private void Start() 17 { 18 19 m_Player = GetComponent<GameObject>(); 20 m_PlayerScript = m_PlayerScript.GetComponent<PlayerMove>(); 21 m_HPimage = gameObject.GetComponent<Image>(); 22 m_HPimage.fillAmount = 1.0f; 23 } 24 25 // Update is called once per frame 26 void Update() 27 { 28 29 if (m_PlayerScript.HP <= 0) ←エラーが起きる箇所 30 { 31 m_HPimage.fillAmount = 0; 32 } 33
・参照先 プレーヤースクリプト using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMove : MonoBehaviour { private JudgeToAvoid m_Judge; // 敵の攻撃判定 private Controller m_Control; // プレーヤー入力 private int m_attackPoint; // 攻撃力 private int m_hp; // 体力 public int AttackPoint { get { return m_attackPoint; } set { m_attackPoint = value; } } public int HP { get { return m_hp; } set { m_hp = value;} } //Use this for initialization void Start() { AttackPoint = 10; HP = 100; m_Control = GetComponent<Controller>(); m_Judge = gameObject.transform.Find("Avoid_area").GetComponent<JudgeToAvoid>(); }
***追加分 HPイメージスクリプト**** using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class HpGauge : MonoBehaviour { [SerializeField] private GameObject m_Player; private PlayerMove m_PlayerScript; private Image m_HPimage; private void Start() { m_PlayerScript = m_Player.GetComponent<PlayerMove>(); m_HPimage = gameObject.GetComponent<Image>(); m_HPimage.fillAmount = 1.0f; } // Update is called once per frame void Update() { if (m_PlayerScript.HP <= 0) { m_HPimage.fillAmount = 0; } while (m_PlayerScript.HP > 0) { m_HPimage.fillAmount -= 1.0f / m_PlayerScript.HP; if (m_PlayerScript.HP <= 10) { // 10以下になったら点滅する Flashing(m_HPimage.color); } } }
試したこと
シリアライズ化して、インスペクター上で紐付け。 ⇒ 結果 NULL
タグでオブジェクトを検索し、スクリプト内で参照 ⇒ 結果 プレーヤーの子オブジェクトまで取得しようとしているのか、画面が固まりました…
補足情報(FW/ツールのバージョンなど)
Unity2017.2
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/09/15 03:53
2018/09/15 06:53
2018/09/15 10:19 編集