Forum Discussion
rajajms
1 year agoExplorer
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?
2 Replies
Replies have been turned off for this discussion
- rajajmsExplorer
using Oculus.Interaction; using Oculus.Interaction.Surfaces; using System.Collections; using UnityEngine; public class RayInteractionSetup : MonoBehaviour { Collider _meshCollider; ColliderSurface _colliderSurface; RayInteractable _rayInteractable; InteractableUnityEventWrapper _interactableUnityEventWrapper; PointableUnityEventWrapper _pointableUnityEventWrapper; 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); StartCoroutine(Temp()); } private IEnumerator Temp() { yield return new WaitForSeconds(1f); _interactableUnityEventWrapper.WhenHover.AddListener(Hover); } private void Hover() { Debug.Log("Hover"); } }I managed to resolve the issue by adding a small wait after setting up the InteractableUnityEventWrapper. It seems that the component needed a moment to initialize properly before I could attach the listener to the WhenHover event.
- rajajmsExplorer
hvox1138 knows any solution to this?
I'm facing an issue with my Unity code where the functionality only works when the GameObject is selected in the hierarchy. Specifically, _interactableUnityEventWrapper.WhenSelect is throwing a NullReferenceException if the GameObject is not selected.
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device
Other Meta Support
Related Content
- 4 years ago
- 3 years ago