Question by
AmazingCore_Gaming · May 20, 2017 at 10:30 AM ·
c#2d gamecollider2d
NullreferenceException dealing damage to a child of the enemy
So i know people get this a lot but i cant find an answer. In my 2d top down game to create the animation for the wings i had to create childs with and empty object with a box collider 2D. In my code its set to damage anything with the tag "Enemy" but if i set the enemy itself i can only damage it if i hit the box collider on the enemy itself and cant damage any of the child colliders, but if i set the child colliders to the tag "Enemy" then when i attack it i get the nullreferenceexception error. Anyone know how i cant fix this? i have my code bellow (the error is on this line other.gameObject.GetComponent().HurtEnemy(currentDamage);) and and image of my enemy showing my childs.
public class HurtEnemy : MonoBehaviour {
public int damageToGive;
private int currentDamage;
public Transform hitPoint;
public GameObject damageNumber;
private PlayerStats thePS;
// Use this for initialization
void Start () {
thePS = FindObjectOfType<PlayerStats>();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.tag == "Enemy")
{
//Destroy(other.gameObject);
currentDamage = damageToGive + thePS.currentAttack;
other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(currentDamage);
var clone = (GameObject) Instantiate(damageNumber, hitPoint.position, Quaternion.Euler(Vector3.zero));
clone.GetComponent<FloatingNumbers>().damageNumber = currentDamage;
}
}
}
bat-childs.png
(35.0 kB)
Comment