So I’m working on a little FPS and right now I have one enemy. When I kill that enemy it says "Object reference not set to an instance of the object. This is the area of code that the error is highlighting:
Vector3 firePointPos = new Vector3(firePoint.position.x, firePoint.position.y, firePoint.position.z);
RaycastHit hit;
Debug.DrawRay(firePointPos, transform.forward, Color.cyan);
if (Physics.Raycast(firePointPos, transform.forward, out hit))
{
Debug.Log(hit.collider.tag);
}
Enemy enemy = hit.collider.GetComponent<Enemy>();
if (enemy != null)
{
enemy.DamageEnemy(damage);
Debug.Log("We hit " + hit.collider + " and did " + damage + " damage");
}
}`
More specifically it is highlighting this line:
Enemy enemy = hit.collider.GetComponent<Enemy>();