Reset AR Session

Is there a way to reset the AR session?

I tried to use this in, but it seems it does not do anything:
ARSubsystemManager.DestroySubsystems();
ARSubsystemManager.CreateSubsystems();

Sometimes the tracking goes wrong or when reopening an app all es messed up and I want to give the users to reset the scene. Then all planes have to removed and the tracking points and so on.
For ARKit I had a script that can do this, but now with foundation it seems a bit more difficult.

You should be able to destroy the ARSession GameObject and Instantiate a new one to achieve this.

Thanks. Will try that.
And then it is just a matter of destroying the visual planes to have a clean new session?

Did this work for you? I tried this but when but i get this error when enabeling the newly spawned arsession: InvalidOperationException: Cannot start AR session because there is no session subsystem

Same here, “InvalidOperationException: Cannot start AR session because there is no session subsystem.” when enabling ARSession. I also get it if I load from SampleScene to another scene and then try to load SampleScene again.

Update: Exception only occurs in the Editor.

Hi. Did destroying the AR Session object and re-instantiating work for anyone? I get a black screen when I do that.

I’m also getting a black screen, instead of the camera feed, after destroying the ARSession component and then adding it back to the same gameObject.

I couldn’t find a nice way to reset the session. For now I get all the planes and point clouds and destroy/disable them manually.

If I’m reloading AR scene for the second time, I’m getting the "Cannot start AR session…" error, and device is not tracking any planes, due to session fail

I just did a test on a Pixel where I enabled/disabled the ARSession component, and also Destroyed and Instantiated it. All seemed to work as expected.

For this particular issue:

What device and OS version?

I have the same issue when loading scene again. Just one note that I’m using multiple scenes loaded additively and AR session lives in the scene from scene load.

I can’t already reproduce this exact error, it’s been lost in the iterations of fixing attempts.
The thing is I was trying to fix nonexistent error, because it was only in the Editor, as soon as I built my app for device, everything went okay.
I used directives for ARSubsystemManager functions, because all of the systems are null in the editor, therefore it can’t function properly.

So it looks like this and works like charm even when reloading scene:

void Start()
    {
#if !UNITY_EDITOR
        ARSubsystemManager.DestroySubsystems();
        ARSubsystemManager.CreateSubsystems();
        ARSubsystemManager.StopSubsystems();
        arSession.gameObject.SetActive(false);
#endif
    }

public async void StartTracking()
    {
#if !UNITY_EDITOR
        arSession.gameObject.SetActive(true);
        ARSubsystemManager.StartSubsystems();
        arPlaneManager.planeAdded += PlaneTracked;
#else
        //here comes some emulation of plane tracking for Editor:
        //PlaceInfinitePlane(Vector3.zero);
        //await new WaitForSeconds(1f);
        //OnPlaneTracked?.Invoke();
#endif
    }

May it help someone to figure out how to handle reload of ar session =)

1 Like

Same here. I think multiple scenes is breaking something.

We’re also having the same issue with reloading the scene a second time and getting a black screen. Samsung S8 and Android, and iPhone 7 Plus and iOS 12

2 Likes

Same problem here

For your information, there is a method Reset() on ARSession component.
See https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@1.0/api/UnityEngine.XR.ARFoundation.ARSession.html
Seems to work as expected :slight_smile:

2 Likes

But how we can call that method? I have a button that calls “ResetDemo” function to load the firstScene and reset the ARKit but doestn works :

using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
using UnityEditor;
using UnityEngine.SceneManagement;

public class HumanBodyTracker : MonoBehaviour
{
....
    private void Reset() {
         Debug.Log("Reseting....");
    }

    public void ResetDemo()
    {
        Reset();
        SceneManager.LoadScene("StartScene");
        Debug.Log("Loading Start Scene...");
        message = "Loading Start Scene...";

    }

You have to get a reference to the ARSession in your scene to be able to call the Reset(). You can either get it by placing your script on the same GameObject, as the ARSession is on

ARSession arSession = GetComponent<ARSession>();
arSession.Reset();

or you can link it by inserting a serialized field and set the reference in the inspector

using UnityEngine.XR.ARFoundation;

[SerializeField]
private ARSession _arSession;

and call it in your method as follows

_arSession.Reset();

Unfortunately this Reset() doesn’t solve the problem with the black screen in my project… So I raise my hand to say: “Same problem here”:wink:

2 Likes

Thanks, works like “its reseting” but same issue, black screen I think that maybe we need to store the gameobjects inside of the session…

The Reset() works fine, if you don’t leave the scene and stay in the AR-scene. As I can see from your posting you are then also loading another scene within your special reset, so this causes the black screen. Comment that out, so your app stays in the same scene and try the reset again.

The problems stays the same: If you leave an AR-scene and later return to an AR-scene the background is just black :frowning: