title: Unintentionally destroying BoxCollider in Unity
body: <p>I have a scene in Unity 2018.2.8f1, based off the <code>AvatarGrab</code> sample scene (this is in Assets/Oculus/SampleFramework/Usage, with the "Oculus Integration" package), where the user is supposed to be able to create, move, and delete objects in the scene. My problem is that after using <code>Instantiate</code> to create an object at the position of one of the hands, and then using <code>Destroy</code> to remove it, the user is not able to grab any other objects in the scene. Here is a minimal script that is attached to the scene:</p>
  
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Creator : MonoBehaviour {
  
      public GameObject leftHandAnchor;
      public GameObject rightHandAnchor;
      public GameObject ballPrefab;
  
  void Update () {
  
      if (OVRInput.GetDown(OVRInput.RawButton.B)) {
          if (rightHandAnchor != null) {
              AddBall(rightHandAnchor);
          }
      }
      if (OVRInput.GetDown(OVRInput.RawButton.Y)) {
          if (leftHandAnchor != null) {
              AddBall(leftHandAnchor);
          }
      }
      if (OVRInput.GetDown(OVRInput.RawButton.A)) {
          if (rightHandAnchor != null) {
              RemoveBall(rightHandAnchor);
          }
      }
      if (OVRInput.GetDown(OVRInput.RawButton.X)) {
          if (leftHandAnchor != null) {
              RemoveBall(leftHandAnchor);
          }
      }
  }
  
  void AddBall (GameObject anchor) {
          GameObject clone = (GameObject)Instantiate(ballPrefab, anchor.transform.position, anchor.transform.rotation);
  }
  
  void RemoveBall (GameObject anchor) {
      RaycastHit[] hits = Physics.SphereCastAll(anchor.transform.position, 0.03f, anchor.transform.forward, 0f);
      foreach (RaycastHit ball in hits){
          if (ball.transform.gameObject.tag == "BALL") {
              Destroy(ball.transform.gameObject);
          }
      }
  }
  }
  </code></pre>
  
  <p>The actual script is much longer, and is <a href="https://github.com/jlazovskis/vipics/blob/master/Scripts/Baller.cs" rel="nofollow noreferrer">accessible here</a>.</p>
  
  <p>The <code>leftHandAnchor</code> prefab is <code>hand_left</code> from the <code>LocalAvatarWithGrab</code> part of the mentioned scene, and <code>rightHandAnchor</code> is <code>hand_right</code>. The <code>ballPrefab</code> is the default GameObject -> 3D Object -> Sphere, with <code>Box Collider</code>, <code>Rigidbody</code> (gravity off, kinematic on), and <code>OVRGrabbable</code> components added.</p>
  
  <p>So the user creates the balls with <code>B</code> or <code>Y</code>, and is able to move them around by grabbing with the index triggers and letting go. Then, say, <code>A</code> is pressed on the right hand controller when its near a ball to remove (and it does get destroyed). But then, if the user tries to use the index trigger (on the right hand) to move another ball, I get the following error in the console:</p>
  
  <p><a href="https://i.stack.imgur.com/xo1Ip.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xo1Ip.png" alt="enter image description here"></a></p>
  
  <p>The left hand index trigger still works to move any object, but as soon as <code>X</code> on the right hand controller is used to delete an object, no other objects can be grabbed, just like with the right hand. </p>
  
  <p>My guesses for the cause of the problem are that either:</p>
  
  <ol>
  <li><code>SphereCastAll</code> is picking up the collider of the hands and deleting it, or</li>
  <li>the ball prefab is inheriting the collider from the anchor, and destroying the ball is destroying the hand as well.</li>
  </ol>
  
  <p>To fix this, I have tried the following:</p>
  
  <ul>
  <li>Add a <code>BALL</code> tag to the ball prefab, only destroy if <code>SphereCastAll</code> picks up an object with that tag.</li>
  <li>Check if <code>BoxCollider</code> and <code>gameObject</code> are not <code>null</code> before destroying the ball.</li>
  <li>Pass a vector to <code>RemoveBall</code> instead of the hand's <code>GameObject</code>, then do <code>SphereCastAll</code> near that vector.</li>
  </ul>
  
  <p>None of these have fixed the problem. My current solution is to destroy the <code>MeshRenderer</code> of the ball and turn on gravity so that the ball drops to the bottom of the scene. This is fine for now, but with every ball more objects are added (can expect it to get to ~10 000 objects), and the scene gets quite slow, so I would like to be able to cleanly destroy the balls. So my question is:</p>
  
  <blockquote>
    <p>How do I destroy a<code>GameObject</code> without destroying the hand that is
    destroying it?</p>
  </blockquote>