- Home /
 
How to instantiate an Oculus grabbable object at runtime in Unity?
Hi everyone,
Right now I have an obj file in Unity. I have a script that adds a Collider component, a Rigidbody component, and finally an OVRGrabbable component to the object. I need to add these components at runtime because eventually I will be producing procedural meshes in a script at runtime, and I need these procedural meshes to be grabbable.
My problem is that the OVRGrabbable script does not recognize the added collider as a grab point when the collider is added at runtime. I thought that it would be enough to add the collider before the OVRGrabbable in my script, but no dice. I tried attaching the collider in an Awake function and then the OVRGrabbable in the Start function, but that didn't work either. Additionally, I cannot add it in script because the grabPoints array is read-only. Here is my code:
     public class AddVRComponents : MonoBehaviour {
         void Start () {
             public bool freeMoving = false;
             public bool useGravity = false;
      
             collide = gameObject.AddComponent<BoxCollider>();
            
             Rigidbody rB = gameObject.AddComponent<Rigidbody>();
             if (!freeMoving)
             {
                 rB.drag = Mathf.Infinity;
                 rB.angularDrag = Mathf.Infinity;
             }
             if (!useGravity)
             {
                 rB.useGravity = false;
             }
      
             OVRGrabbable grab = gameObject.AddComponent<OVRGrabbable>();
             Collider[] newGrabPoints = new Collider[1];
             newGrabPoints[0] = collide;
             grab.enabled = true;
             grab.grabPoints = newGrabPoints;
         }
     }
 
 
               This obviously does not work because the final line produces the error that grab.grabPoints is read-only.
I know that it can be done because if I run my program and then in the editor manually drag my collider into the grab points field of the OVRGrabbable component, the object can be grabbed.
How can I get the OVRGrabbable script to recognize my collider?
Sorry for the late reply. I figured it out for single colliders. Just add this code snippet to the OVRGrabbable script:
     virtual public void CustomGrabCollider(Collider collider)
     {
         m_grabPoints = new Collider[1] { collider };
     }
 
                   And then you call that wherever you generate your object and send the object's collider as argument.
Still need to get it for compound colliders. Asked the question here: https://answers.unity.com/questions/1597777/oculus-sdk-how-to-add-colliders-to-ovrgrabbable-at.html
Your answer