Unity Crashes when Manually Loading XR

Hello all.

I am currently developing an application that is meant to be compatible with both regular 3D and XR. That is, an application with both regular scenes and XR scenes. To facilitate this behavior, I have disabled “Initialize XR on Startup” in the OpenXR settings.

To manually load XR, I use the following code (from the most recent Unity docs).

public IEnumerator StartXRCoroutine()
{
Debug.Log(“Initializing XR…”);
yield return XRGeneralSettings.Instance.Manager.InitializeLoader();

    if (XRGeneralSettings.Instance.Manager.activeLoader == null)
    {
        Debug.LogError("Initializing XR Failed. Check Editor or Player log for details.");
    }
    else
    {
        Debug.Log("Starting XR...");
        XRGeneralSettings.Instance.Manager.StartSubsystems();
    }
}

However, the “yield return” statement just causes Unity to crash. I know it is that statement because I have commented everything below it out.

It should not be a hardware issue with my VR headset since everything runs fine when “Initialize XR on Startup” is active.

Any help on this would be greatly appreciated.

Here is the stack trace for the error, in case it will be of use:

Received signal SIGSEGV
Obtained 60 stack frames
0x00007ffaca0f6ad7 (LibOVRRT64_1) ovr_ReleaseHapticsClip
0x00007ffaca0be902 (LibOVRRT64_1) ovr_ReleaseHapticsClip
0x00007ffac6b368c6 (OVRPlugin) ovrp_UnityOpenXR_OnSessionDestroy
0x00007ffaf3798e45 (UnityOpenXR) main_UnloadOpenXRLibrary
0x0000021e7572d8b7 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.XR.OpenXR.OpenXRLoaderBase:Internal_PumpMessageLoop ()
0x0000021e7572d7f3 (Mono JIT Code) [OpenXRLoader.cs:298] UnityEngine.XR.OpenXR.OpenXRLoaderBase:ProcessOpenXRMessageLoop ()
0x0000021e7506dc62 (Mono JIT Code) UnityEngine.BeforeRenderHelper:Invoke ()
0x0000021e7506d9fb (Mono JIT Code) UnityEngine.Application:InvokeOnBeforeRender ()
0x0000021e837907ed (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
0x00007ffab16ffe24 (mono-2.0-bdwgc) mono_jit_set_domain
0x00007ffab163e764 (mono-2.0-bdwgc) mono_object_get_virtual_method
0x00007ffab163e8fc (mono-2.0-bdwgc) mono_runtime_invoke
0x00007ff6134b7614 (Unity)
0x00007ff613496eb4 (Unity)
0x00007ff61318a61b (Unity)
0x00007ff613f578db (Unity)
0x00007ff61445865e (Unity)
0x00007ff612b7aef9 (Unity)
0x0000021e7506d928 (Mono JIT Code) (wrapper managed-to-native) UnityEditor.EditorGUIUtility:RenderPlayModeViewCamerasInternal_Injected (UnityEngine.RenderTexture,int,UnityEngine.Vector2&,bool,bool)
0x0000021e7506d853 (Mono JIT Code) UnityEditor.EditorGUIUtility:RenderPlayModeViewCamerasInternal (UnityEngine.RenderTexture,int,UnityEngine.Vector2,bool,bool)
0x0000021e7506c593 (Mono JIT Code) UnityEditor.PlayModeView:RenderView (UnityEngine.Vector2,bool)
0x0000021e70cb11d3 (Mono JIT Code) UnityEditor.GameView:OnGUI ()
0x0000021e70c8deea (Mono JIT Code) UnityEditor.HostView:InvokeOnGUI (UnityEngine.Rect)
0x0000021e70c8dd43 (Mono JIT Code) UnityEditor.DockArea:DrawView (UnityEngine.Rect)
0x0000021e70c80afb (Mono JIT Code) UnityEditor.DockArea:OldOnGUI ()
0x0000021e70c6ac3f (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoOnGUI (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,bool,UnityEngine.Rect,System.Action,bool)
0x0000021e70c6a01b (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:HandleIMGUIEvent (UnityEngine.Event,UnityEngine.Matrix4x4,UnityEngine.Rect,System.Action,bool)
0x0000021e7500286b (Mono JIT Code) UnityEngine.UIElements.IMGUIContainer:DoIMGUIRepaint ()
0x0000021e75000524 (Mono JIT Code) UnityEngine.UIElements.UIR.RenderChainCommand:ExecuteNonDrawMesh (UnityEngine.UIElements.UIR.DrawParams,single,System.Exception&)
0x0000021e74fff6b3 (Mono JIT Code) UnityEngine.UIElements.UIR.UIRenderDevice:EvaluateChain (UnityEngine.UIElements.UIR.RenderChainCommand,UnityEngine.Material,UnityEngine.Material,UnityEngine.Texture,UnityEngine.Texture,single,Unity.Collections.NativeSlice1<UnityEngine.UIElements.UIR.Transform3x4>,Unity.Collections.NativeSlice1,UnityEngine.MaterialPropertyBlock,bool,System.Exception&)
0x0000021e74ff9fc3 (Mono JIT Code) UnityEngine.UIElements.UIR.RenderChain:Render ()
0x0000021e8265a893 (Mono JIT Code) UnityEngine.UIElements.UIRRepaintUpdater:Update ()
0x0000021e70c4ec1e (Mono JIT Code) UnityEngine.UIElements.VisualTreeUpdater:UpdateVisualTreePhase (UnityEngine.UIElements.VisualTreeUpdatePhase)
0x0000021e8265a02b (Mono JIT Code) UnityEngine.UIElements.Panel:UpdateForRepaint ()
0x0000021e82658d2b (Mono JIT Code) UnityEngine.UIElements.Panel:Repaint (UnityEngine.Event)
0x0000021e70c4e4cf (Mono JIT Code) UnityEngine.UIElements.UIElementsUtility:DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel)
0x0000021e70c4e223 (Mono JIT Code) UnityEngine.UIElements.UIElementsUtility:UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (int,intptr,bool&)
0x0000021e70c4e01f (Mono JIT Code) UnityEngine.UIElements.UIEventRegistration:ProcessEvent (int,intptr)
0x0000021e70c4df5b (Mono JIT Code) UnityEngine.UIElements.UIEventRegistration/<>c:<.cctor>b__1_2 (int,intptr)
0x0000021e70c4ddbd (Mono JIT Code) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
0x0000021e70c4de76 (Mono JIT Code) (wrapper runtime-invoke) :runtime_invoke_void_int_intptr_intptr& (object,intptr,intptr,intptr)
0x00007ffab16ffe24 (mono-2.0-bdwgc) mono_jit_set_domain
0x00007ffab163e764 (mono-2.0-bdwgc) mono_object_get_virtual_method
0x00007ffab163e8fc (mono-2.0-bdwgc) mono_runtime_invoke
0x00007ff6134b7614 (Unity)
0x00007ff613496eb4 (Unity)
0x00007ff613491ac5 (Unity)
0x00007ff6135b411a (Unity)
0x00007ff613fef5e8 (Unity)
0x00007ff61453a31b (Unity)
0x00007ff613fef3fa (Unity)
0x00007ff61453314e (Unity)
0x00007ff61453c55d (Unity)
0x00007ff6140c3067 (Unity)
0x00007ff6140c106f (Unity)
0x00007ff61450ff5a (Unity)
0x00007ff61451480b (Unity)
0x00007ff6158a20ce (Unity)
0x00007ffb4f887034 (KERNEL32) BaseThreadInitThunk
0x00007ffb50542651 (ntdll) RtlUserThreadStart

@DemonReborn
I didn’t include this in my first post but I do call the coroutine from the following method.

public void LoadVrFromNonVr()
{
    Debug.Log("Attempting to enter XR mode...");

    StartCoroutine(StartXRCoroutine());
}

The issue is that whenever this call is made, Unity crashes.

It looks like the issue may be caused by a bug in the Unity engine itself. The yield return statement in your code appears to be correct and should not cause Unity to crash.

One potential solution to this issue may be to update to the latest version of Unity. The crash you are experiencing may have been fixed in a newer version of the engine. You can check for updates by going to the Unity Hub and clicking on the “Installs” tab, then selecting your project and clicking on the “Check for updates” button.

Another potential solution is to try using an older version of Unity that is known to be stable and compatible with your project. You can do this by clicking on the “Installs” tab in the Unity Hub, selecting your project, and then clicking on the “Add” button to add a new version of Unity to your project. You can then switch between different versions of Unity by clicking on the “Switch” button next to the installed version.

If none of these solutions work, you may want to try reaching out to the Unity support team for further assistance. They may be able to provide additional information or help you troubleshoot the issue further.

To use this code, you would need to call the coroutine in your script. This can be done by using the StartCoroutine method and passing the name of the coroutine as a string. For example:

StartCoroutine("StartXRCoroutine");

Alternatively, you can use a lambda expression to start the coroutine, like this:

StartCoroutine(() => {
    Debug.Log("Initializing XR...");
    yield return XRGeneralSettings.Instance.Manager.InitializeLoader();

    if (XRGeneralSettings.Instance.Manager.activeLoader == null)
    {
        Debug.LogError("Initializing XR Failed. Check Editor or Player log for details.");
    }
    else
    {
        Debug.Log("Starting XR...");
        XRGeneralSettings.Instance.Manager.StartSubsystems();
    }
});

Once the coroutine is started, it will run the code inside of it to initialize and start the XR subsystems. This will allow you to use XR features in your application.