Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug XR Simulation fails to start a second time in Play Mode

Discussion in 'AR' started by Xtro, Sep 25, 2023.

  1. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    596
    Hello.

    Earlier, I posted this to XR Interaction Toolkit forum by mistake so, I'm moving it to the AR forum now. Sorry.

    I am having a problem with XR simulation in the editor. First, it works fine but when I reload my AR scene, the simulation can't initialize itself.

    Here is my setup:
    I have 2 scenes: AR and NoAR.
    My app switches between these 2 scenes when the user demands it.
    AR scene contains the AR systems and it all works well.
    AR scene is programmed to download and show different construction projects to the user based on their selection.

    When the user goes to NoAR scene and comes back to the AR scene later for switching to a different construction project, I get this error below from the XR simulation.

    Does anyone have an idea?
    Is this a bug?
    Does Unity XR simulation not support re-loading a scene containing the AR systems?

    MissingReferenceException: The object of type 'UnityEngine.Camera' has been destroyed but you are still trying to access it.

    Call stack:
    Code (CSharp):
    1. UnityEngine.Object+MarshalledUnityObject.ThrowNullExceptionObjectImpl (System.Object obj) (at <affe600311164ff58161da271ceee4f0>:0)
    2. UnityEngine.Object+MarshalledUnityObject.MarshalNullCheck[T] (T obj) (at <affe600311164ff58161da271ceee4f0>:0)
    3. UnityEngine.Camera.get_cullingMask () (at <affe600311164ff58161da271ceee4f0>:0)
    4. UnityEngine.XR.Simulation.SimulationSessionSubsystem+SimulationProvider.Start () (at ./Library/PackageCache/com.unity.xr.arfoundation@5.0.7/Runtime/Simulation/Subsystems/SimulationSessionSubsystem.cs:78)
    5. UnityEngine.SubsystemsImplementation.SubsystemWithProvider`3[TSubsystem,TSubsystemDescriptor,TProvider].OnStart () (at <dd5e9aedb1ee4e01890b1500bced999d>:0)
    6. UnityEngine.SubsystemsImplementation.SubsystemWithProvider.Start () (at <dd5e9aedb1ee4e01890b1500bced999d>:0)
    7. UnityEngine.XR.ARFoundation.ARSession.StartSubsystem () (at ./Library/PackageCache/com.unity.xr.arfoundation@5.0.7/Runtime/ARFoundation/ARSession.cs:396)
    8. UnityEngine.XR.ARFoundation.ARSession+<Initialize>d__39.MoveNext () (at ./Library/PackageCache/com.unity.xr.arfoundation@5.0.7/Runtime/ARFoundation/ARSession.cs:384)
    9. UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <affe600311164ff58161da271ceee4f0>:0)
    10. UnityEngine.XR.ARFoundation.ARSession:OnEnable() (at ./Library/PackageCache/com.unity.xr.arfoundation@5.0.7/Runtime/ARFoundation/ARSession.cs:351)
     
    OnTopStudios likes this.
  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    956
    Our samples repo does this all the time. (The main menu scene is not an AR scene.) How you implement this depends on the needs of your application, but for reference here is how we handle scene changes: https://github.com/Unity-Technologi...ob/5.1/Assets/Scripts/Runtime/SceneUtility.cs

    To assess whether this is a bug or not we would need to know how you implement the scene loading/unloading in your project and what calls you are making to the XRLoader. (https://docs.unity3d.com/Packages/c...r.html#managing-xr-loader-lifecycles-manually)
     
  3. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    596
    Hello. Thank you for your detailed answer. I'll check the sample code and let you know the difference of how I do it.
     
  4. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    974
    I checked the Simple AR Scene from repo for v5.1, but How did you use it?

    By Adding SceneUtility.cs & SceneUtilitySetup.cs to my project, I could get rid of the error.
    My Scene is loaded 2nd time successfully, and shows the simulated environment, cloud points are shown upon Plane Detection, but Planes are not recognized.

    They can be recognized in a random moment after randomly moving in the scene after 15, 20, 60... seconds. I have no issues on Mobiles, only in the Editor.

    So, the issue is the same for 2nd, 3rd, so on reloading of the scene.

    --

    I don't make calls to the XRLoader.
    I open scenes with this code.

    Code (CSharp):
    1.     public int Progress = 0;
    2.     private bool _activeScene = false;
    3.     public delegate void LoadEvent();
    4.     public event LoadEvent OnStartWaitingEventToActivateScene;
    5.  
    6. ----
    7.  
    8.     StartCoroutine((DoLoadLevel("SceneName", "LoadScreen", false));
    9.  
    10. -----
    11.  
    12.     private IEnumerator DoLoadLevel(
    13.         string name, string customLoadScene, bool manualActivation)
    14.     {
    15.         // Reset
    16.         Progress = 0;
    17.         OnStartWaitingEventToActivateScene = null;
    18.         _activeScene = true;
    19.  
    20.         // Load the loading scene
    21.         SceneManager.LoadScene(customLoadScene);
    22.  
    23.         yield return null;
    24.         yield return null;
    25.  
    26.         AsyncOperation async = SceneManager.LoadSceneAsync(name);
    27.  
    28.         if (manualActivation)
    29.         {
    30.             async.allowSceneActivation = false;
    31.  
    32.             _activeScene = false;
    33.         }
    34.  
    35.         while (!async.isDone)
    36.         {
    37.             if (manualActivation)
    38.             {
    39.                 if (async.progress < 0.9f)
    40.                 {
    41.                     Progress = Mathf.RoundToInt(
    42.                         Mathf.Clamp(async.progress / 0.9f * 100f, 0f, 100f));
    43.                 }
    44.                 else
    45.                 {
    46.                     // the first load phase was completed!
    47.                     Progress = 100;
    48.  
    49.                     if (!async.allowSceneActivation)
    50.                     {
    51.                         //  notify the end of first loading phase
    52.                         OnStartWaitingEventToActivateScene?.Invoke();
    53.  
    54.                         while (!_activeScene)
    55.                         {
    56.                             yield return null;
    57.                         }
    58.  
    59.                         async.allowSceneActivation = true;
    60.                     }
    61.                 }
    62.             }
    63.             else
    64.             {
    65.                 Progress = Mathf.RoundToInt(async.progress * 100f);
    66.             }
    67.  
    68.             yield return null;
    69.         }
    70.  
    71.         OnStartWaitingEventToActivateScene = null;
    72.     }
    73.  
     
    Last edited: Dec 13, 2023
  5. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    956
    Yeah we've seen the delay in plane detection times in XR Simulation when a scene is reloaded as well. This hasn't been a high enough priority for us to investigate yet, but feel free to file a bug if you'd like to poke the team to get this fixed: https://unity3d.com/unity/qa/bug-reporting
     
  6. makaka-org

    makaka-org

    Joined:
    Dec 1, 2013
    Posts:
    974
    Additional notice: I don't use XR Interaction Toolkit.