cancel
Showing results for 
Search instead for 
Did you mean: 

Add Object Interactions During Runtime

abdur.rahman.709771
Honored Guest

Hello,

I'm trying to instantiate an object during runtime and need to add interactions to it. Specifically I want to add a mesh collider, rigiidbody, HandGrabInteractable, Grabbable, OneGrabFreeTransformer and TwoGrabFreeTransformer to the object. I can not create a prefab for the object as I'm importing it from a server and adding it to the scene during runtime. The issue I'm currently facing is that though I can add the components during runtime, how do I set the proper references in Grabbable for the transformers and in the HandGrabInteractable to the grabbable. I tried creating a prefab with all of the 5 components attached to it and add it as a child for the 3d object but for some reason this doesn't work.

2 REPLIES 2

Big_Flex
Expert Protege

Hi @abdur.rahman.709771, have you tried using the "Inject" functions in the Interactor and Interactable scripts and their child classes? Those functions will let you specify components at runtime.  

Samer157
Honored Guest

Hi @abdur.rahman.709771 , you need to use the Inject functions to assign the Rigidbody and Grabbable to the HandGrabInteractable component as @Big_Flex mentioned. But the other thing that is needed is to register the interactable with the registry if you're doing this at runtime. For example:

var grabbable = gameObject.AddComponent<Grabbable>();
var rigidBody = gameObject.GetComponent<Rigidbody>(); // or add if you don't have it

handGrabInteractable = gameObject.AddComponent<HandGrabInteractable>();
handGrabInteractable.InjectRigidbody(rigidBody);
handGrabInteractable.InjectOptionalPointableElement(grabbable);
HandGrabInteractable.Registry.Register(handGrabInteractable); // This is the part that's essential to work at runtime.

I had the same issue and was wondering why it won't work at runtime and had to dig to find this. Hope this helps!