Having trouble making a bullet harm enemies c#

using UnityEngine;
using System.Collections;

public class BULLET : MonoBehaviour {
float lifeSpan = 3.0f;
private EnemyHealth enemyHealth;
void Awake(){
enemyHealth = GetComponent ();
}

void Start(){

}

void Update(){
	lifeSpan -= Time.deltaTime;

	if (lifeSpan <= 0) {
		Destroy(gameObject);
	}
}

void OnCollisionEnter(Collision col)
{
	if (col.gameObject.tag == "Enemy") 
	{
		enemyHealth.curHealth -= 10;
	}
}

}

on line 27 “enemyHealth.curHealth -= 10;” there’s an error on unity that says “object reference not set to an instance of an object” I don’t know what’s wrong. sorry if its something obvious, I’m still new to scripting.

it is because enemyHealth is null. You need to assign it. So:

if (col.gameObject.tag == "Enemy") 
{
    enemyHealth = col.gameObject.GetComponent<EnemyHealth>();
    enemyHealth.curHealth -= 10;
}

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<EnemyHealth>().curHealth -= 10;
     }
 }

Or, if you want to get rid of the tag completely:

void OnCollisionEnter(Collision col)
 {
     EnemyHealth enemy = col.GetComponent<EnemyHealth>();
     if (enemy != null) 
     {
             enemy.curHealth -= 10;
     }
 }

try replacing,

enemyHealth.curHealth -= 10;

with,

col.enemyHealth.curHealth -= 10;