Loading a new scene in unity make OVRManager throws errors
I have oculus integration vr 32 in unity 2021.1.16.
If I open a scene, play it with oculus link cable it works fine.
But if I open another scene, either just choosing another scene in unity or load a new scene from in the game, it starts throwing this error: MissingReferenceException: The object of type 'OVRCameraRig' has been destroyed but you are still trying to access it.
I have an OVRCameraRig prefab in each scene (which has OVRCameraRig and OVRManager) which is oculus own prefab.
I suspect that something lingers in OVRManager when I switch scenes. How do you use the OVRManager? Do you create it in 1 scene and then mark it for "DontDelete" or is there something that must be made in OVRManager to stop this error?
Answer by rh_galaxy · Oct 13, 2021 at 10:23 AM
I have never tried having those in every scene so I can't answer on that. I have a GameManager script attached to CameraHolder object as a singleton do not destroy object. I don't even use OVRManager or the OVRCameraRig to keep code compatible with non-oculus versions. I use a normal camera with a TrackedPoseDriver added to the same object like in the image. However should I add OVRManager and OVRCameraRig it would be to the CameraHolder object.
In my Awake() in GameManager I setup everything once and keep it when switching between scenes (I only have two scenes, Menu and Game). Maybe this can give you some ideas at least.
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.XR;
using UnityEngine.InputSystem;
using System.Threading;
public static GameManager theGM = null;
public CameraController theCameraHolder;
//first code to run
void Awake()
{
//singleton
if (theGM == null)
{
theGM = this;
}
else if (theGM != this)
{
//enforce singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject); //<- this makes OnDestroy() be called and
//we don't want to deinit everything there (leave it empty).
return;
}
//the rest is done once only...
DontDestroyOnLoad(gameObject);
bool bInited = InitOculus();
if (!bInited)
{
bNoVR = true;
Screen.SetResolution(1280, 720, true);
Debug.Log("Error initing VR, continue with no VR");
}
else
{
Screen.SetResolution(864, 960, false);
}
//set thread prio
UnityEngine.Application.backgroundLoadingPriority = UnityEngine.ThreadPriority.BelowNormal;
Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal;
//init to black screen
theCameraHolder.InitForMenu();
oFadeMatCopy = new Material(oFadeMat);
oFadeBox.GetComponent<MeshRenderer>().material = oFadeMatCopy;
StartFade(0.01f, 0.0f, true);
//set audio out setting
if (!bNoVR) AudioStateMachine.instance.SetOutputByRiftSetting();
AudioSettings.OnAudioConfigurationChanged += AudioSettings_OnAudioConfigurationChanged;
}
private void AudioSettings_OnAudioConfigurationChanged(bool deviceWasChanged)
{
if (!bNoVR) AudioStateMachine.instance.SetOutputByRiftSetting();
else AudioStateMachine.instance.SetOutput(0);
}
//start of oculus specific code
bool InitOculus()
{
try
{
Core.AsyncInitialize("200555...."); //appid
Entitlements.IsUserEntitledToApplication().OnComplete(EntitlementCallback);
Users.GetLoggedInUser().OnComplete(LoggedInUserCallback);
}
catch (UnityException e)
{
Debug.LogException(e);
UnityEngine.Application.Quit();
}
bOculusDevicePresent = true;
return true;
}
void EntitlementCallback(Message msg)
{
if (msg.IsError)
{
Debug.LogError("Not entitled to play this game");
UnityEngine.Application.Quit(); //it is possible to remove quit while developing
}
else
{
Debug.Log("You are entitled to play this game");
}
}
void LoggedInUserCallback(Message msg)
{
if (msg.IsError)
{
Debug.LogError("No Oculus user");
}
else
{
//save ID, and user name
szUserID = msg.GetUser().ID.ToString();
szUser = msg.GetUser().OculusID;
Debug.Log("You are " + szUser);
bUserValid = true;
}
}