How to destroy a gameObject when hit by Raycast?

After the explosion has been instantiated, I would like to destroy the gameObject I hit. Heres my code:

> if(Input.GetKeyUp(KeyCode.Mouse0)) {
>     		Debug.Log("Shot a bullet");
>     		var fwd = transform.TransformDirection
> (Vector3.forward);
>     		var hit :RaycastHit;
>     		
>     		if (Physics.Raycast (transform.position, fwd, hit, 60)) {
>     			Debug.Log ("Target Hit");
>     			Instantiate(bulletCheck, hit.point, transform.rotation);
>     			Instantiate(explosion, hit.point, transform.rotation);
>                           //Here I want to destroy the gameObject I hit.
>     		}
>     		
>     	}

The RaycastHit structure contains the hit object’s transform, which can be used to access the gameObject property:

    if (Physics.Raycast (transform.position, fwd, hit, 60)) {
        ...
        Destroy(hit.transform.gameObject); // destroy the object hit
    }

Try:

Destroy(hit.collider.gameObject);

1 - Have a gameobject in the scene named “Camera” with a cameracomponent on it.

2 - Make sure there is a collider on the object you try to hit.

http://docs.unity3d.com/412/Documentation/ScriptReference/Physics.Raycast.html - at the bottom of the document.

(C#)

private Transform cameraTransform;
private Camera cam;

void Start(){
    cameraTransform = GameObject.Find("Camera").transform;
    cam = cameraTransform.camera;
}

void Update(){

    if (Input.GetMouseButtonDown(0)) {
    
        Ray ray = cam.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 10000)){
            Debug.DrawLine(ray.origin, hit.point);
            Debug.Log("Clicked on " + hit.transform.gameObject.name); 
            Destroy(hit.transform.gameObject);
        }		
    }
}

if you want to make a game like an ant smash in 3d then this one is helping you

Camera mainCam;
 // target layer set in inspector [ specific object with this layer ]
public LayerMask layer;

private void Awake () {
    mainCam = Camera.main;
}
private void Update () {
    // Calculate the distance
    Ray ray = mainCam.ScreenPointToRay (Input.mousePosition);
    Plane plane = new Plane (Vector3.up, Vector3.forward);
    float rayDistance;

    if(plane.Raycast(ray, out rayDistance)) {
        Vector3 point = ray.GetPoint (rayDistance);
        // Draw a line btw camera to end point
        Debug.DrawLine (ray.origin, point, Color.red);      
    }
    RaycastHit hit;
    if(Physics.Raycast (ray, out hit, rayDistance, layer, QueryTriggerInteraction.Collide)) {
        if(hit.collider != null) {
            Destroy (hit.collider.gameObject);
            Debug.Log (hit.collider.name);
        }   
    }