cancel
Showing results for 
Search instead for 
Did you mean: 

How to instantiate a grabbable object at runtime in Unity?

Anonymous
Not applicable
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?

Thank you all



0 REPLIES 0