I'm assuming the object with the tag "Enemy" have a component EnemyHealth.
If that's the case, you want to access that particular enemy and it's component.
Rather than calling enemyHealth (which is a private property in your bullet-script, but has never been assigned, which causes the null reference), you should write
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Enemy")
{
col.GetComponent().curHealth -= 10;
}
}
Or, if you want to get rid of the tag completely:
void OnCollisionEnter(Collision col)
{
EnemyHealth enemy = col.GetComponent();
if (enemy != null)
{
enemy.curHealth -= 10;
}
}
↧