title: Destroy GameObject when its clicked on
body: <p>I'm making a building mechanic in my game and I want to be able to clear out certain objects around the map (trees, other decor) so I have room to build. I've tried using a ray cast to find what object is being clicked on and destroy it but that doesn't work.</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class ObjectDestroy : MonoBehaviour {
      // Start is called before the first frame update
      void Start () {
  
      }
  
      // Update is called once per frame
      void Update () {
          if (Input.GetMouseButtonDown (0)) {
              Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
              RaycastHit hit;
              Debug.Log (Input.mousePosition);
              if (Physics.Raycast (ray, out hit)) {
                  if (hit.collider.gameObject == gameObject) Destroy (gameObject);
  
              }
          }
  
      }
  }
  </code></pre>