Is it possible to destroy the component "BoxCollider" of the object which was clicked?

Is it possible to receive in the script a link to the component “BoxCollider” of the object on which they clicked and to delete it ? … and that way to make it inactive but to stay in the scene…

On click, you raycast and destroy the BoxCollider component of the first gameobject it hits.

void Update(){
  if (Input.GetMouseButtonDown(0)){
  Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  RaycastHit hit;
  if (Physics.Raycast(ray, out hit)){
    if (hit.collider.GetComponent<BoxCollider>() != null){
      Destroy(hit.collider.GetComponent<BoxCollider>());
    }
  }
}

Thanks a lot for your answer!!! I’ve forgot to say that my game is 2d. Its sea battle.


and the objects in the Hierarchy called Cvadrat(clone) are exactly my coordinates and they appear after I’ve pressed play.

… now I need to prevent opportunity of repitiedly shooting at the same coordinates…

doesn’t work