前提・実現したいこと
矢に当たると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
回答1件
あなたの回答
tips
プレビュー