前提・実現したいこと
unity で矢が落ちてきてプレイヤーに当たるとhpゲージが減るscriptでエラーが出ました。
発生している問題・エラーメッセージ
エラーメッセージ NullReferenceException: Object reference not set to an instance of an object ArrowController.Update () (at Assets/ArrowController.cs:32)
該当のソースコード
ソースコード GameDirector.cs 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("hpGauge"); } // Update is called once per frame public void DecreaseHp(){ this.hpGauge.GetComponent<Image>().fillAmount -= 0.1f; } } ArrowController.cs 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); } } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。