Getting Error "Object reference not set to an instance of an object"

Getting this error when I left click and it says it is in the lines 28 and 15 so I have no idea what to do. Any help will do.

using UnityEngine;
using System.Collections;

public class PlayerShooting : MonoBehaviour {

public float firerate = 0.5f;
float cooldown = 0;
public float damage = 25f;

// Update is called once per frame
void Update () {

cooldown -= Time.deltaTime;
if (Input.GetButton (“Fire1”)) {
fire ();

}

}
void fire(){
if (cooldown > 0) {
return;

}

Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
Transform hitTransform;
Vector3 hitPoint;

hitTransform = FindClosestHitObject (ray, out hitPoint);

if (hitTransform != null) {
Debug.Log (“Fired Gun”);

Health h = hitTransform.GetComponent();

while(h == null && hitTransform.parent){
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent();
}

if(h !=null){
h.TakeDamage(damage);
}

}

cooldown = firerate;
}

Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint){

RaycastHit[ ] hits = Physics.RaycastAll (ray);

Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;

foreach (RaycastHit hit in hits) {
if(hit.transform != this.transform && (closestHit==null || hit.distance < distance )){

closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}
}

Is the script attached to a camera? At line 28, it looks like its trying to access a camera property.

Please use code tags, otherwise it just looks like an unreadable mess.

It doesn’t need to be, Camera.main is a static method that returns the first camera that has the tag “MainCamera” in the scene and is enabled. If that is the line that is hitting the error though then that means there is no camera in the scene with that tag or the camera with that tag is disabled (or any camera with that tag), as it would return null which would give that exception.

Also seconded on using code tags.