Access InteractionEvents in code

I have a problem I can’t wrap my head around. I want to define what you can do in the Unity Editor easily by clicking the plus in the InteractionEvents window of XRGrabableInteractable

After this line of code:

myGameObject.GetComponent<UnityEngine.XR.Interaction.Toolkit.XRGrabInteractable>().OnHoverEnter() = (myMeshRenderer.material = (Material)Resources.Load("myMaterial"));

I get this error:
error CS0122: 'XRBaseInteractable.OnHoverEnter(XRBaseInteractor)' is inaccessible due to its protection level
Here is the reference for the script I’m accessing:
https://docs.unity3d.com/Packages/com.unity.xr.interaction.toolkit@0.0/api/UnityEngine.XR.Interaction.Toolkit.XRBaseInteractor.html
Somebody on the discord pointed out that “m_HoverTargets” from XRBaseInteractor is protected but even if it’s the source of the problem, I don’t know how to solve it.

6679483--765472--Capture.PNG

The portion of your code: .OnHoverEnter() is actually calling this method. This method exists and is protected since it has the purpose of raising the event to the event handlers. What you are looking for is the public event.

myGameObject.GetComponent<UnityEngine.XR.Interaction.Toolkit.XRGrabInteractable>().onHoverExited += (e) => Debug.Log(e);

Thanks for the reply!
However, I couldn’t implement the code you provided, though I noticed that the XREvent I’m using is spelled differently (with a lowercase o). It seems this event is public but I don’t know what to do from here because now I get a conversion error: Cannot implicitly convert type 'UnityEngine.Material' to 'UnityEngine.XR.Interaction.Toolkit.XRInteractableEvent'
How do I write it in a way that it adds it as an Event to onHoverEnter?
Edit: onHoverEnter doesn’t seem to be an EventHandler unlike normal UnityEvents which could use onHoverEnter += setMaterial_onHoverEnter; with the code after += being a method.

Ah. It appears that these are not native .NET events indeed. You can probably still achieve what you want by using AddListener() and Invoke().

Unity - Scripting API: Events.UnityEvent.AddListener (unity3d.com)

I’ve tried implementing it but it demands a UnityAction of the type XRBaseInteraction. Namely:
error CS1503: Argument 1: cannot convert from 'void' to 'UnityEngine.Events.UnityAction<UnityEngine.XR.Interaction.Toolkit.XRBaseInteractor>'
I’ve tried rewriting the method to a UnityAction but it needs the part in the <>. If I include that part also it can’t work like a regular method and it then changes to being an “iterator block” (whatever that means). It seems the XR plugin has its own EventHandler as well as its own UnityAction type which are different from the regular ones. Is there any documentation about those? I couldn’t find anything about them on my own.
Edit: For my purposes, I’ll try creating my own UnityEvent instead of using the special one from XRGrabInteractable. It’s a bit sad to have to resort to this though since it could have been easier if it had been documented.

For an example of how to add listeners to the events, see this script from our Examples project. It uses the events on XRBaseInteractable, but it should be exactly the same as for an XRBaseInteractor. This is also using the latest version of the XR Interaction Toolkit which has a different event signature than the version you seem to be on, but the concept is similar.

@chris-massie Thank you for the help! I was having trouble visualizing how to set up events and listeners this makes a lot more sense seeing the example provided.