敵を攻撃して体力を減らして敵を倒す
機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object MyStatus.GetAttackPower () (at MyStatus.cs:36) AttackSword.OnTriggerEnter (UnityEngine.Collider col) (at AttackSword.cs:19)
該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3 4public class MyStatus : MonoBehaviour 5{ 6 7[SerializeField] 8 private int hp; 9 // 力 10 private GameObject equip; 11 [SerializeField] 12 private int power; 13 private WeaponStatus weaponStatus; 14 15 public void SetHp(int hp) 16 { 17 this.hp = hp; 18 } 19 20 public int GetHp() 21 { 22 return hp; 23 } 24 public void SetEquip(GameObject weapon) 25 { 26 equip = weapon; 27 weaponStatus = equip.GetComponent<WeaponStatus>(); 28 } 29 public GameObject GetEquip() 30 { 31 return equip; 32 } 33// 自身の力と武器の攻撃力を合わせたダメージ力を返す 34public int GetAttackPower() 35 { 36 return power + weaponStatus.GetAttackPower(); 37 } 38} 39 40using UnityEngine; 41using System.Collections; 42 43public class AttackSword : MonoBehaviour 44{ 45 private MyStatus myStatus; 46 47 private void Start() 48 { 49 myStatus = transform.root.GetComponent<MyStatus>(); 50 } 51 void OnTriggerEnter(Collider col) 52 { 53 if (col.tag == "Enemy") 54 { 55 var enemyScript = col.GetComponent<Enemy>(); 56 if (enemyScript.GetState() != Enemy.EnemyState.Damage && enemyScript.GetState() != Enemy.EnemyState.Dead) 57 { 58 col.GetComponent<Enemy>().TakeDamage(myStatus.GetAttackPower(), col.ClosestPointOnBounds(transform.position)); 59 } 60 } 61 } 62} 63
試したこと
当たり判定やHP出力などに問題はありませんでした。たぶんこのNullReferenceExceptionが問題だと思います。
どうか解決方法をよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/30 16:03