NullReferenceException in RayInteraction Setup at runtime
I’m working on setting up ray-based interaction at runtime using the Oculus Interaction SDK, but I’m encountering a NullReferenceException in the following line of my code:
_interactableUnityEventWrapper.WhenHover.AddListener(Hover);
Here’s the script I’m using:
using Oculus.Interaction;
using Oculus.Interaction.Surfaces;
using UnityEngine;
public class RayInteractionSetup : MonoBehaviour
{
Collider _meshCollider;
ColliderSurface _colliderSurface;
RayInteractable _rayInteractable;
InteractableUnityEventWrapper _interactableUnityEventWrapper;
private void Start()
{
_meshCollider = gameObject.AddComponent<BoxCollider>();
_colliderSurface = gameObject.AddComponent<ColliderSurface>();
_colliderSurface.InjectCollider(_meshCollider);
_rayInteractable = gameObject.AddComponent<RayInteractable>();
_rayInteractable.InjectSurface(_colliderSurface);
_interactableUnityEventWrapper = gameObject.AddComponent<InteractableUnityEventWrapper>();
_interactableUnityEventWrapper.InjectInteractableView(_rayInteractable);
// This is where the NullReferenceException occurs
_interactableUnityEventWrapper.WhenHover.AddListener(Hover);
}
private void Hover()
{
Debug.Log("Hover");
}
}
It seems like the WhenHover event is not being properly initialized, causing the NullReferenceException. I’ve made sure to add the InteractableUnityEventWrapper and inject the RayInteractable into it, so I’m not sure why this is happening.
Any suggestions on what I might be missing or how to resolve this?
