External script to trigger Interactable

afterthecloud

New member
Are there any tutorials or examples of how to get an external object to trigger an interactable (such as the Shop or Crafting Stable in the demo scene) from a c# script without using a collider trigger?

Looking to open both via a keypress instead of by colliding.

The interact method takes a IInteractor interactor, but how would one invoke this if not using an actual gameobject.
 

afterthecloud

New member
Ok, I figured it out. I'll share it here in case anyone else is interested. Not great naming, but hopefully it makes sense.

Code:
GameObject OpsiveObjectWithInteractable; 
GameObject playerInteracting;
var opsiveGameObject = OpsiveObjectWithInteractable.GetComponent<Interactable>();
var interactor = playerInteracting.GetComponentInParent<IInteractor>();
opsiveGameObject.Interact(interactor);

It could be done without referencing the object externally, but in my use case I wanted it as generic as possible.

playerInteracting = the main player
OpsiveObjectWithInteractable = the game object inside the Shop or Crafting Stable that contains the Interactable script.

This works on any object that uses the Interactable script, so could be used for doors, etc.

This simply allows me to bind a keypress to open the Shop whenever I want, anywhere in the scene.
 

Sangemdoko

Moderator
Staff member
I'm glad you found a solution
If you want you can bypass the interactable comonenet and directly open the menu using the ShopMenuOpener and CraftingMenuOpener components (Use can check out the source if you are curious, they are super simple). The result will be the same.
But as you said your solution would work with any interactable making it more generic
 
Top