I’m trying to created a continuous raycast from the center of the viewport that triggers a script on the other object it hits (to activate UI and a few other functions. The idea is that there are lots of objects in the AR scene and the user reveals UI/information about them by pointing their camera towards it. I’m essentially trying to achieve this effect but within an AR scene:
I have the script working on a Desktop Build with an FPS Controller and have it working with AR Foundation if the raycast is triggered by touching the screen, but anytime I try to have it work without input from the user other than moving it around I get this error in the debug: NullReferenceException: Object reference not set to an instance of an object. Here’s my code (color Change is the script on the object that activates the UI and other “selected” functions): ```csharp
**using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class raycastSelectionAR : MonoBehaviour
{ [SerializeField] private Color highlightMaterial;
[SerializeField] private Color defaultMaterial;
[SerializeField] private string selectableTag= “Subjects”;
[SerializeField] private Camera cam;
//public GameObject UIElements;
void Awake(){
cam = GetComponent();
}
void Update()
{
var ray =cam.ViewportPointToRay(new Vector3(0.5f,0.5f,0));
RaycastHit hit;
if(Physics.Raycast(ray, out hit))
{
var selection = hit.transform;
if (selection.CompareTag(selectableTag))
{
Debug.Log(“cast has hit a profile graphic!”);
Debug.Log(hit.collider.gameObject.name);